Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 498
duality_ Avatar asked Feb 04 '13 19:02

duality_


1 Answers

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.

like image 59
pkat Avatar answered Sep 17 '22 09:09

pkat