Check if polymorphic relation exists returns empty

Posted 2 months 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');
}

migration:

Schema::create('notifications', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('emitter_id')->unsigned()->index()->nullable();
            $table->string('emitter_type')->nullable();
            $table->timestamps();
        });

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.

Reply to

Use Markdown with GitHub-flavored code blocks.