使用Laravel 9.* vue和inertiajs,我正在尝试更新现有的用户信息

yzuktlbb  于 2022-12-14  发布在  Vue.js
关注(0)|答案(1)|浏览(183)

这是错误:来自日志:

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)

用户控制器存储函数:

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');
});

提交按钮功能:

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

Consol错误:

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

VM3897:14590 Uncaught SyntaxError: Invalid or unexpected token

Uncaught TypeError: window.ignite is not a function

我试着改变路线方法修补但没什么变化

5ssjco0h

5ssjco0h1#

  • 在后端 *

如果尝试更新用户数据,则需要其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/

相关问题