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条)

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?