Know if there are related elements in a [closed] table

0

I am using one-to-many relationships in the models and I wanted to know how to be sure there are no related items in the table.

For example, how can I find that the song record with id 3 is not within the musical genre table?

    
asked by Carlos Alexander Lemus Mojica 27.06.2017 в 17:07
source

1 answer

3

What you're looking for is doesntHave :

App\Genero::doesntHave('canciones')->get();

However, if you need to make a specific query in the song relationship, it would be something like this:

Genero::whereDoesntHave('canciones', function ($query) {
    $query->where('id', 3);
})->get();

You can see more information, as always, in the documentation: link

    
answered by 27.06.2017 / 17:11
source