Matt Brunmeier
Matt Brunmeier

Reputation: 1310

Laravel saving hasMany relationship

I'm using Laravel & Ardent and having issues saving through the hasMany relationship.

I have two classes.

Workorder:

protected   $table          = 'workorder';
protected   $primaryKey     = 'workorder_id';
public static $relationsData = [
    'dates'         => array(self::HAS_MANY, 'App\WorkOrderDate', 'foreignKey' => 'workorder_id'),
];

WorkOrderDate:

protected   $table          = 'workorder_date';
protected   $primaryKey     = 'date_id';
public static $relationsData = array(
    'workorder'  => array(self::BELONGS_TO, 'App\WorkOrder')
);  

after saving my Workorder (which works fine), I'm trying to save a collection of Date models by executing:

$workorder->dates()->saveMany($myDateCollection->all());

But it's not running the date insert, and it's not giving me any errors. I turned on query logging and it looks like Laravel is trying to run an update instead of an insert. Do I have my relationship defined incorrectly here?

Upvotes: 1

Views: 1007

Answers (1)

Matt Brunmeier
Matt Brunmeier

Reputation: 1310

I was hydrating my date collection from user input, and when you hydrate a model, Laravel assumes the model exists already. Turns out this is not what I mean to do.

More detail here: https://github.com/laravel/framework/issues/9360

Upvotes: 1

Related Questions