【发布时间】:2015-01-05 21:28:27
【问题描述】:
我正在查看 Laravel 中的批量分配,并尝试为一个模型及其相关模型批量分配数据。
我的模型:
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();
}
}
我的输入
我的Input:all() 看起来像这样,来自表单:
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"
}}}
我现在该怎么做才能将数据实际保存为两个新的数据库行? (一个新闻,一个内容)
我认为现在它会很简单:
$news = News::create(Input::all());
$news->push();
但我显然遗漏了一些东西。
我收到错误:preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
批量分配对相关模型根本不起作用吗?
或者它可以正常工作,但不能与 morphMany 关系?
我误解了$model->push(),还是$model::create?
提前致谢。
【问题讨论】:
标签: php laravel eloquent polymorphic-associations