laravellaravel-4eloquent

How to update column value in laravel


I have a page model. It has following columns in database table:

I want to update only "image" column value.

Here's my code:

public function delImage($path, $id) {
    $page = Page::find($id);
    $page->where('image', $path)->update(array('image' => 'asdasd'));
    \File::delete($path);
}

it throws me an error, that i am trying to use where() on a non-object. How can i correctly update my "image" column value?


Solution

  • You may try this:

    Page::where('id', $id)->update(array('image' => 'asdasd'));
    

    There are other ways too but no need to use Page::find($id); in this case. But if you use find() then you may try it like this:

    $page = Page::find($id);
    
    // Make sure you've got the Page model
    if($page) {
        $page->image = 'imagepath';
        $page->save();
    }
    

    Also you may use:

    $page = Page::findOrFail($id);
    

    So, it'll throw an exception if the model with that id was not found.