mercredi 11 novembre 2020

Laravel query to list results

I am trying to write a query which takes a collection of records and loops through them and displays them all but i need it to hide a specific record if one record contains a field as true.

Use Case

  1. Grab collection of products where the purchased field is true
  2. if a product from the collection has the field includes_bonus set to true list ALL products apart from the bonus product which has the id of 2

1.

This grabs all the products that have purchased set to true

$productsPurchased = $user->products()->where('purchased', 1)->get();

2 attempt

Below i trying to do the above but im not having much luck.

  1. Grab all products with purchased set to 1(true)

  2. if product includes_bonus as 1(true) hide product record with id of 2 from list of products.

    $productsPurchased = $user->products()->where('purchased', 1)
        ->where('includes_bonus', 1)
        ->where('products.id', '!==', 2)
        ->get();
    

blade

I then wanna do a foreach within the blade which lists all the products with my use case.

   @foreach($purchasedProducts as $purchasedProduct)
                <div class="col-xl-6 p-0 p-xl-4 mb-5 mb-xl-0">
                        <div class="card border">
                            <div class="card-header border rounded-top"
                               </div>
                            <div class="card-block">
  @endforeach

Aucun commentaire:

Enregistrer un commentaire