// this will return a collection of just IDs
$editableStockItemIds = $product->stockItems()->available()->pluck('id');
// and then
dd($editableStockItemIds->contains($request->stockItem));
Assuming that the stockItem from the request is a single ID not an array of IDs.
so $request->stockItem is an array of Ids from the form?
You want to check if ANY of these are in the available stock items?
OR you want to check that ALL of these are in the available stock items?
OR you want to check that NONE of these are in the available stock items?
One approach is to use the request ids in a whereIn eloquent query. The result you get back will be no records, some records but a different count, or exactly the same count. Use this depending on which of the above questions you want to answer
@snapey I want to make sure that ALL of these are in the available stock items.
Some kind of "validation" for the IDs from the form, to make sure they all are part of the product itself (stockItems of the product).
So maybe as suggested. Count the IDs provided, query the database, count the result. If the counts are the same then your query of the database was an accurate match.
//make sure all IDs from POST request are part of the stockItems which are available for the selected product
abort_unless(collect($request->stockItem)->keys()->diff($editableStockItemIds)->isEmpty(), 404);
@geordiejackson I'm checking if the posted ids from form (array) are all in a collection (editableStockItemIds).
This is a check to make sure, the ids of the items a user is trying to edit are somehow "related" to the users products.