Answer the question
In order to leave comments, you need to log in
How to check the existence of records in the database?
I started learning Laravel and ran into a problem. There is the following code
/**
* @param int $category_id
* @param int $product_id
*
* @return JsonResponse
*/
public function destroy(int $category_id, int $product_id): JsonResponse
{
$user = Auth::getUser();
$category = $user->categories()->find($category_id);
if (!$category) {
return response()->json([
'message' => 'Category not found.'
], 404);
}
$product = $category->products()->find($product_id);
if (!$product) {
return response()->json([
'message' => 'Product not found.'
], 404);
}
$product->delete();
return response()->json([
'message' => 'Product deleted.'
], 200);
}
Answer the question
In order to leave comments, you need to log in
There is something like this =)
But naturally, it will not magically find user categories by category, there is no such strong magic here.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question