加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel Eloquent $model-> save()不保存但没有错误

发布时间:2020-12-14 19:45:18 所属栏目:大数据 来源:网络整理
导读:更新我的Post模型时,我运行: $post-title = request('title');$post-body = request('body');$post-save(); 这不会更新我的帖子.但它应该根据the Laravel docs on updating Eloquent models.为什么我的模型没有更新? 我没有错误. 该帖子未在db中更新. 除了
更新我的Post模型时,我运行:
$post->title = request('title');
$post->body = request('body');

$post->save();

这不会更新我的帖子.但它应该根据the Laravel docs on updating Eloquent models.为什么我的模型没有更新?

>我没有错误.
>该帖子未在db中更新.
>除了没有在数据库中更新,没有别的东西似乎奇怪.没有错误.行为正常.
> running this test to see if save succeeded的结果是真的.
> This Laravel thread没有帮助

发布模型:

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

   ....
}

邮政控制人:

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('/');
}

奖金信息

运行dd($post-> save())返回true.

运行

$post->save();

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

向我显示$fetchedPost与之前的帖子相同而没有更新的数据.

由于Laravel 5.5 laravel已经改变了一些验证机制,我想你需要尝试这种方式.
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',]);
    } else {
      $validatedData = $request->validate([
        'body' => 'required|min:19',]);
    }

    $post->update($validatedData);

    return redirect('/');
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读