I have question that How can I check using Laravel Query in Product Display Controller that the display product is listed in Favourite table.
My Database Structure is
- Favourite Table
— productid — customer id - Product Table
— productid — productname - Customer Table
— Customer ID — Customer Name
I need a query to show all products with favourite icon display on every product, if the customer added in favourite list the favourite icon will be red
I made conditions in view page inside loop but is it possible to check in query and display with column name isFavourite?
like
@foreach(productsDB as productsRow)
@if (productsRow == FavouriteRow)
<i class="favourite-red">icon-heart</i>
@else
<i class="favourite-white">icon-heart</i>
@endif
@endfor
2
Answers
Yes, you can achieve this by using Laravel’s Eloquent ORM to perform the necessary queries.
Assuming you have models for Product, Favourite, and Customer, and they are properly related, you can do something like this:
This query will fetch all products along with an additional column
isFavourite
which will be null for non-favorite products and will contain the favorite ID for favorite products.In your Blade template, you can then use this information to display the favorite icon:
This assumes you have properly set up the relationships in your models, Controllers and that you have a way to retrieve the current authenticated user’s ID. If not, you may need to adjust the code accordingly.
These are model relations:
This is the query:
in your blade you can write simply:
This logic will make your blade file cleaner.