1 year ago

Check if polymorphic relation exists returns empty

Posted 1 year ago by MarkyzZz

Basically, i have model Notification that morphs to two other models (Company and Scooter), i called them "the emitter". The problem is that i cannot get the notification for the companies or scooters that exist. It always returns me empty collection. This is my code: Notification.php :

public function emitter()
    return $this->morphTo();

Scooter.php and Company.php:

public function notifications()
    return $this->morphMany(Notification::class, 'emitter');


Schema::create('notifications', function (Blueprint $table) {

And the queries i've tried:

$notification = Notification::has('emitter')->get(); // returns empty collection
$notification = Notification::whereHas('emitter', function($inner_query){ return $inner_query->exists(); })->get(); //same

Edit: Forgot to mention that i am using Laravel 5.2

Please sign in or create an account to participate in this conversation.