Laravel Eloquent $model->save() not saving but no error

48,140

Solution 1

Check your database table if the 'id' column is in uppercase 'ID'. Changing it to lower case allowed my save() method to work.

Solution 2

I had the same and turned out to be because I was filtering the output columns without the primary key.

$rows = MyModel::where('...')->select('col2', 'col3')->get();
foreach($rows as $row){
    $rows->viewed = 1;
    $rows->save();
}

Fixed with

$rows = MyModel::where('...')->select('primary_key', 'col2', 'col3')->get();

Makes perfect sense on review, without the primary key available the update command will be on Null.

Solution 3

I had the same problem and changing the way I fetch the model solved it!

Was not saving even though everything was supposedly working just as you have mentioned:

$user = User::find($id)->first(); 

This is working:

$user = User::find($id);

Solution 4

You have to make sure that the instance that you are calling save() on has the attribute id

Solution 5

Since Laravel 5.5 laravel have change some validation mechanism I guess you need to try this way.

public function store(Request $request, $id)
{
    $post = Post::findOrFail($id);

    $validatedData = [];

    // Request validation
    if ($post->type == 1) {
        // Post type has title
        $validatedData = $request->validate([
          'title' => 'required|min:15',
          'body' => 'required|min:19',
      ]);
    } else {
      $validatedData = $request->validate([
        'body' => 'required|min:19',
    ]);
    }

    $post->update($validatedData);

    return redirect('/');
}
Share:
48,140
Jacob
Author by

Jacob

Updated on July 09, 2022

Comments

  • Jacob
    Jacob almost 2 years

    When updating my Post model, I run:

    $post->title = request('title');
    $post->body = request('body');
    
    $post->save();
    

    This does not update my post. But it should according to the Laravel docs on updating Eloquent models. Why is my model not being updated?

    Post model:

    class Post extends Model
    {
        protected $fillable = [
            'type',
            'title',
            'body',
            'user_id',
        ];
    
       ....
    }
    

    Post controller:

    public function store($id)
    {
        $post = Post::findOrFail($id);
    
        // Request validation
        if ($post->type == 1) {
            // Post type has title
            $this->validate(request(), [
                'title' => 'required|min:15',
                'body' => 'required|min:19',
            ]);
    
            $post->title = request('title');
            $post->body = request('body');
        } else {
            $this->validate(request(), [
                'body' => 'required|min:19',
            ]);
    
            $post->body = request('body');
        }
    
        $post->save();
    
        return redirect('/');
    }
    

    Bonus info

    Running dd($post->save()) returns true.

    Running

    $post->save();
    
    $fetchedPost = Post::find($post->id);
    dd($fetchedPost);
    

    shows me that $fetchedPost is the same post as before without the updated data.