简体   繁体   中英

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

this is the error: from log:

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)

UserController Store 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));
    }

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

submit button function:

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

the Consol error:

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

VM3897:14590 Uncaught SyntaxError: Invalid or unexpected token

Uncaught TypeError: window.ignite is not a function

I tried to change the route methode to patch nothing changed

In your backend

If you try to update user data then you need its id

public function store(Request $request, $id) 

and modify your route

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

Then use it in your method like below

$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!');

In your front end

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

Inside your component

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

Then you can access it like below

this.userupdate

You can take a look at this example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM