skip to Main Content

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

  1. Favourite Table
    — productid — customer id
  2. Product Table
    — productid — productname
  3. 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


  1. 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:

    use AppModelsProduct;
    use AppModelsFavourite;
    
    $customerId = auth()->user()->id; // Assuming you have authentication and getting customer ID
    
    $products = Product::select('products.*', 'favourites.id as isFavourite')
        ->leftJoin('favourites', function($join) use ($customerId) {
            $join->on('products.productid', '=', 'favourites.productid')
                 ->where('favourites.customer_id', '=', $customerId);
        })
        ->get();
    

    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:

    @foreach($products as $product)
        @if ($product->isFavourite !== null)
            <i class="favourite-red">icon-heart</i>
        @else
            <i class="favourite-white">icon-heart</i>
        @endif
    @endforeach
    

    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.

    Login or Signup to reply.
  2. These are model relations:

    class Product extends Model {
        public function favourites() {
            return $this->hasMany(Favourite::class, 'productid');
        }
    }
    
    class Favourite extends Model {
        public function product() {
            return $this->belongsTo(Product::class, 'productid');
        }
    
        public function customer() {
            return $this->belongsTo(Customer::class, 'customerid');
        }
    }
    

    This is the query:

    $customer = Auth::user();
    
    $products = Product::select('products.*')
        ->leftJoin('favourites', function($join) use ($customer) {
            $join->on('products.productid', '=', 'favourites.productid')
                ->where('favourites.customerid', '=', $customer->id);
        })
        ->addSelect(DB::raw('CASE WHEN favourites.productid IS NOT NULL THEN 1 ELSE 0 END AS isFavourite'))
        ->get();
    

    in your blade you can write simply:

    @foreach($products as $product)
        @if ($product->isFavourite)
            <i class="favourite-red">icon-heart</i>
        @else
            <i class="favourite-white">icon-heart</i>
        @endif
    @endforeach
    

    This logic will make your blade file cleaner.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search