I'm looking at mass assignment in Laravel, and trying to mass assign data for both one model and it's related model.
My models:
class News extends Eloquent {
protected $table = 'news';
protected $fillable = array(
'title', 'slug', 'author', 'img', 'content',
);
public function content() {
return $this->morphMany('Content', 'morphable')->orderBy('section');
}
}
class Content extends Eloquent {
protected $table = 'contents';
protected $fillable = array(
'rawText', 'section',
);
public function morphable() {
return $this->morphMany();
}
}
My Input
I have Input:all()
looking like this, coming from the form:
array(6) {
["_token"]=>
string(40) "irrelevant"
["title"]=>
string(11) "Happy Title"
["author"]=>
string(9) "Mr. Happy"
["slug"]=>
string(11) "happy-title"
["img"]=>
string(9) "happy.png"
["content"]=>
array(1) {
[0]=>
array(2) {
["section"]=>
string(4) "body"
["rawText"]=>
string(27) "# I'm happy! ## So happy"
}}}
What do I do now to actually save the data as two new database rows? (one in news, one in contents)
I thought it would now be as simple as:
$news = News::create(Input::all());
$news->push();
But I'm clearly missing something.
I get the error: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
Does mass assignment not work at all with related models?
Or does it work fine, but not with morphMany relations?
Have I misunderstood $model->push()
, or $model::create
?
Thanks in advance.