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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With