问题
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