1
I am trying to fetch items that are not in a particular product.
I therefore have a model Produto, each product has several items, and another model Item.
I’m trying to use Collections of Laravel, more specifically the method diff, as follows:
public function buscarItens($id)
{
$produto = Produto::find($id);
//busca todos os itens cadastrados
$todosItens = Item::all();
//busca os itens do produto
$itensDoProduto = $produto->itens;
//retorna os produtos que NÃO PERTENCEM ao item + os produtos que PERTENCEM ao item
$collection1 = collect($todosItens);
$diff = $collection1->diff($itensDoProduto);
return response()->json($collection1);
}
It turns out that this difference that is returning is equal to itself $todosItens, as if there were no items in common between the $itensDoProduto and $todosItens, but there is.
What might be going on?
OBS: I thought I’d use the diffKeys(), but I want to seek only the difference of ids, and not of all attributes.