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:
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:
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!
the create does the save method you do not need to do it again you can do it as the following:
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)
];
$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) {
echo "Saved";
}
else {
echo "NO SAVE";
}
dd($tasks, "DONE");
}