Laravel 4: save a polymorphic relationship's record: undefined method newQuery()

柔情痞子 提交于 2019-12-10 15:34:42

问题


I have a polymorphic relationship in Laravel 4 and it's working like I want. I have another one, which doesn't work when I try to insert the related model, even though it is identical to the first one and I use it in the same way.

I have Event model and an Article model:

// Article Model
class Article extends Eloquent {
    public function events() {
        return $this->morphMany('Event', 'eventable');
    }
    ...

// Event Model
class Event extends Eloquent {
    public function eventable() {
        return $this->morphTo();
    }
}

Usage:

$article = Article::find($article_id);
// insert
// neither of the following lines work
$article->events()->create(array("type" => "click"));
$article->events()->save(new Event(array("type" => "click")));

I get the same error:

Error: Call to undefined method Illuminate\Support\Facades\Event::newQuery() in
\app_path\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php line 383

What am I doing wrong? I have an identical situation with an Article and it's Photos, but there it works (it's also a polymorphic relation).


回答1:


Event is likely a reserved word in Laravel. Unless you namespace it, i'd suggest avoiding creating a class called Event, since it is like used in Laravel core. Something like activity could potentially serve as an alternative.



来源:https://stackoverflow.com/questions/14694319/laravel-4-save-a-polymorphic-relationships-record-undefined-method-newquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!