如何更新laravel中的列值 [英] How to update column value in laravel

查看:121
本文介绍了如何更新laravel中的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面模型。它在数据库表中有以下列:




  • id

  • title

  • image

  • 正文



我只想更新image列值



这是我的代码:

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

它会给我一个错误,我试图使用where()一个非对象。如何正确更新我的图像列值?

解决方案

您可以尝试以下方式:

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

还有其他方法,但不需要使用 Page :: find $ id); 在这种情况下。但是如果你使用 find(),那么你可以这样尝试:

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

//确保你有页面模型
if($ page){
$ page-> image ='imagepath';
$ page-> save();
}

另外您可以使用:

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

所以,如果没有找到那个id的模型,它会抛出异常。 >

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

  • id
  • title
  • image
  • body

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?

解决方案

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.

这篇关于如何更新laravel中的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆