When the content is stored (new) it has no id. You first have to store the content before storing data into the pivot.
Swap theese lines
$content->menu()->sync($input);
Content::create($input);
Reply if it hepled
What happens when you try this...
$content = Content::create($input);
dd($content->id);
$content->menu()->sync($input);
T2theC is right. Inside your original code the object stored in the database (result of Content::create) is not assigned to a variable. Sorry, I didn't explore the code enought.
Now we know the problem is that your $content doesn't know it's id after creating.
Is the model successfully saved? Can you find it in the database?
Maybe the guarded is causing problems (probably not). Try remove it for a single test.
When I save the data, the data gets saved, but there is no data in the content_menu table which refrences the content and menu tables. I removed the guarded line and and I still got a null.
Hey Quite not sure of my proposal but here it is
Try to replace Content::create($input);
with
$content->fill($attributes)->save(); dd($content->id);
(with replacing $attributes by $input directly or after a manipulation, i didnt took time to compare $input and $attributes type and structure)
This is what I get when I replace
Content::create($input);
With
$content->fill($input)->save(); dd($content->id);
int 16
I tried to save my stuff without the dd($content-id)
and it kind of worked.
It saved, but there was at least 3 extra rows that was saved in the pivot table.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community