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?
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?
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