duanlei5339 2018-07-25 19:26
浏览 188
已采纳

Laravel模型没有保存,但它说它(Observer类内)

I have a task model and I am trying to do a simple create to test something in one of my observer classes. Here's my create code.

private function generateTasks(Event $event)
{
    $tasks = [];

    $tasks[] = [
        'event_id' => $event->id,
        'title' => 'This is a test',
        'description' => 'this is a description',
        'due_on' => Carbon::now()->addDays(14)
    ];

    dump(Task::all());
    // foreach ($tasks as $task) {
        $task = Task::create([
            'event_id' => $event->id,
            'title' => 'This is a test',
            'description' => 'this is a description',
            'due_on' => Carbon::now()->addDays(14)
        ]);

        if ($task->save()) {
            echo "Saved";
        }
        else {
            echo "NO SAVE";
        }

        dump($task);
    // }

    dd($tasks, "DONE");
}

And the output is below:

enter image description here

When I look at the database the table remains at 120 rows. This is really odd because it says it's saving. When I grab all the tasks you can see in the output it has 120 rows. If you look at my latest output id you'll see that I am up to 133.

Any ideas what is going on here?

UPDATE:

My fillables:

protected $fillable = [
    'title', 'description', 'due_on', 'event_id', 'completed'
];

Completed has a default of false.

My table looks like this to give you guys an idea:

enter image description here

  • 写回答

2条回答 默认 最新

  • dstt1818 2018-07-26 13:26
    关注

    I ended up getting help from the laravel chat. Big shout-out to them. The problem was that I was using a dump die inside of a transaction. This was causing what appeared to be a save but then, because the die was called before the transaction commit, it was rolling everything back. It was a silly mistake. Thanks for all the help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况