TransWikia.com

Call to a member function update() on null Laravel function

Stack Overflow Asked by Piotrek on November 4, 2021

What is wrong with this function? I ve got error on this line:
$user->update($input); Call to a member function update() on null

$input = $request->all();

if ($input == NULL) {
    return redirect()->route('users.index');
}

else {
    
    $user = User::find($id);
    
    $user->update($input);
    
    DB::table('model_has_roles')->where('model_id', $id)->delete();
    
    $user->assignRole($request->input('roles'));
    
    return redirect()->route('users.index')
        ->with('success', 'User updated successfully');
    
}

One Answer

You can check if user has some data. Or you can findOrFail() instead of find().

$input = $request->all();

if ($input == NULL) {
    return redirect()->route('users.index');
}

else {
    
    $user = User::find($id);
    if ($user) {
        $user->update($input);
        
        DB::table('model_has_roles')->where('model_id', $id)->delete();
        
        $user->assignRole($request->input('roles'));
    }
    
    return redirect()->route('users.index')
        ->with('success', 'User updated successfully');
    
}

Answered by Habib on November 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP