Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cooperino's avatar

What is the correct query to check if a list of items is empty

I am getting an array of ids of lists of items. And I need to check that there aren't empty lists. I have a list_to_items table with a corresponding ListToItems model (pivot) that has list_id and item_id columns, where each row specifies which item_id belongs to which list_id. So if there aren't any rows with the list_id it means it's empty.

$array_of_items_lists= $request['lists'];
foreach ($array_of_items_lists as $list){
   if (ItemsList::find($list)->list_to_items()->...?
}

Or I should do something like:

ListToItem::wherehas(...)

I am confused because it's a many to many relationship and I am not sure how should I check that (items and lists have their own tables and models and all have the relationships set up correctly)

0 likes
10 replies
Nakov's avatar

If it is many to many that means that each list has an array of items..

So in your List model you should have :

public function items()
{
	return $this->belongsToMany(Item::class);
}

given the fields and the table name are following Laravel's naming convention https://laravel.com/docs/8.x/eloquent-relationships#many-to-many-polymorphic-relations

Then from what I see above, you have an array of lists or whatever that is, I hope it is IDs of lists that you have:

So you can do the following:

$listsWithItems = List::has('items')->with('items')->whereIn('id', $request['lists'])->get();

Here again I am assuming that this $request['lists'] is an array of LIST IDs that you have in your database.

So the $listsWithItems will return back only those lists that have any items, and it will return the items with them.

Hope this answers your question or at least gives you a path on where to look at.

1 like
cooperino's avatar

@Nakov thank you, I will try.

Is what I tried to do also correct?

foreach ($array_of_items_lists as $list){
   if (ItemsList::find($list)->items_to_list()->isEmpty()) {
     // it is empty
    }
}

Because I need to return an error if any of the lists is empty, not just retrieve the data

Nakov's avatar
Nakov
Best Answer
Level 73

@cooperino so if there are 100 list items, you will make 100 queries with your approach:

Now this is a fast response, but I might do just two queries to first test if there are lists with no items:

$noItemsList = List::doesntHave('items')->whereIn('id', $request['lists'])->count();

if ($notItemsList)
{
    // it is empty
}
1 like
Snapey's avatar

@cooperino if you ever have database queries inside a loop then that is a definite sign that you need to refactor your code

1 like
cooperino's avatar

@Snapey Definitely, I felt something is wrong and I'm just starting with eloquent and Laravel in general. Now I know. Thank you, you are really helping me here on the forum

cooperino's avatar

@nakov what I don't understand is how you didn't use the pivot table here, since the list table itself does not contain a column with the item ids, it just has the user id who created it, then the pivot table items_to_list has the list_id and the item_id

Nakov's avatar

@cooperino It is good if you read my responses. In the first response I gave you all you need to use the correct naming convention so that everything I showed you will just work..

In your List model add this:

public function items()
{
	return $this->belongsToMany(Item::class, 'items_to_list');
}

And please watch some videos, read something from the documentation, it is bad nowadays with so much resources/videos available to not start by learning first, and straight jumping into deep waters. The forum here is to guide you on what to do, not to lead you step by step on every blocker that you have.. You'll never learn that way.

1 like
cooperino's avatar

@Nakov I do read really! I already have that relationship, I just got confused since you used it as a parameter with('items'), and I was sure I have to use relationships like List::find()->items() or sort. (I am really new with eloquent and query builder)

So we completely skip the items_to_list relationship?

Nakov's avatar

@cooperino I am afraid you read but you don't understand, you are really new to eloquent and I really hope that you'll start by reading the docs, watching some videos as well.

The items function that I provided for you above, don't you see that it has a second parameter which is the table that you are naming? So items() will be using that table for the check..

1 like
cooperino's avatar

@Nakov Oh yes. And now that I look at the code, this is exactly the same relationship as you wrote, I just missed the fact that the first parameter is the Item class, but the second one is the pivot table, and not the items table.

I will read more. Btw, it is working, thank you!

Please or to participate in this conversation.