簡體   English   中英

使用 Laravel 9.* vue 和 inertiajs,我正在嘗試更新現有的用戶信息

[英]using Laravel 9.* vue and inertiajs and i am trying to update an existing user info

這是錯誤:來自日志:

local.ERROR: Call to undefined method App\Models\User::user() {"userId":14,"exception":"[object] (BadMethodCallException(code: 0): Call to undefined method App\\Models\\User::user() at /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:71)

用戶控制器存儲 function:

    public function Store(Request $request) 
    {
        $request->validate([
            'first_name' => ['required', 'string', 'max:255'],
            'last_name' => ['required', 'string', 'max:255'],
            // 'location' => ['required', 'string', 'max:255'],
        ]);
        $user = \App\Models\User::user()->update($request->all());
        if ($user) {
            $message = 'Account updated successfully.';
        } else {
            $message = 'Error while saving. Please try again.';
        }
        return redirect()->route('profileupdate.user')->with('message', __($message));
    }

路線 web.php:

Route::prefix('profile')->name('profile')->group(function() {
    Route::get('/', [UserController::class, 'Profile'])->name('user.profile');
    Route::put('/', [UserController::class, 'Store'])->name('update.user');
});

提交按鈕 function:

const submitProfile = () => {
            profileForm.put(route("profileupdate.user"));
        };

控制台錯誤:

PUT http://localhost/profile 500 (Internal Server Error)

VM3897:14590 Uncaught SyntaxError: Invalid or unexpected token

Uncaught TypeError: window.ignite is not a function

我試圖改變路由方法來修補沒有改變

在你的后端

如果您嘗試更新用戶數據,那么您需要它的 ID

public function store(Request $request, $id) 

並修改你的路線

Route::put('/users/{id}/', 'UserController@store')->name('userupdate');

然后在您的方法中使用它,如下所示

$user = \App\Models\User::findOrFail($id);

$user->update([
  'first_name' => $request->first_name,
  'last_name' => $request->last_name
]);

return back()->with('status', 'The user data has been updated!');

在你的前端

<component user-route="{{ route('userupdate') }}"></component>

在你的組件內部

<script> 
    export default {
        props: ['userupdate'],
    }
</script>

然后你可以像下面這樣訪問它

this.userupdate

你可以看看這個例子

https://kollox.com/how-to-pass-laravel-route-to-a-vue-component/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM