How to update column value in laravel

后端 未结 3 1151
小鲜肉
小鲜肉 2020-12-23 19:49

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

  • id
  • title
  • image
  • body

I want to update only \"image\"

3条回答
  •  感情败类
    2020-12-23 20:24

    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.

提交回复
热议问题