duanlei5339 2018-07-25 11: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 05: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部