Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

You are using insert() method of the Query Builder, instead of save() method of the Model. Insert is a basic insert statement and it does not know about relations, timestamps or anything else that is specific to the Eloquent Model. So simply use this code:

Author::find(1)->posts()->save(
  new Post(array('title' => 'My first post ever', 'body' => 'This is going to be the best Eloquent lesson!'))
);
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jjosephs jjosephs Joined 14 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.