doujia9833
2018-07-03 09:53
浏览 59
已采纳

Laravel 5.6 - 只有在更改后才能保存

I have a Laravel 5.6 project and am trying to update a post from a form like this

<input name="email" type="text" class="form-control" placeholder="Email" value="{{$user->email}}" required>

In my PostsController I have this...

    if($request->input('email')) {
        $user->email = $request->input('email');
    }

I wanted it to only update the email if a new one has been input in the form, but because I am pre-populating the form fields it always passes the IF.

Is there a more Laravel way to do this? I have looked at isDirty() but I am not sure it is the correct thing to use.

图片转代码服务由CSDN问答提供 功能建议

我有一个Laravel 5.6项目,我正在尝试从这样的表单更新帖子 <前> &lt; input name =“email”type =“text”class =“form-control”placeholder =“Email”value =“{{$ user-&gt; email}}”required&gt; \ n

在我的PostsController中我有这个......

  if($ request-&gt; input('email)  ')){
 $ user-&gt; email = $ request-&gt; input('email'); 
} 
   
 
 

我只想要它 如果在表单中输入了新的电子邮件,则更新电子邮件,但由于我预填充表单字段,因此它总是通过IF。

是否有更多Laravel方法可以执行此操作 ? 我看过isDirty()但是我不确定它是否正确使用。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpouda6700 2018-07-03 13:06
    已采纳

    The save() method will check if something in the model has changed. If there is no change it won't run a db query. So you don't need to explicitly write a code for that. Still if you want to use isDirty method you can write as follows

    if($test->isDirty()){
        // changes have been made
    }
    

    Or if you want to check a certain attribute:

    if($test->isDirty('email')){
       // email has changed
    }
    
    点赞 评论

相关推荐 更多相似问题