0

I want to show the ProductAttributes relation based on the product's attribute_status column.

Here is my code:

Product::with('productImages', 'productReviews', 'user.vendors', 'subchildcategories', 'ProductAttributes')
    ->where('category_id', $request->category_id)
    ->where('products.status', 1)
    ->get();
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Rahumathulla
  • 73
  • 10
  • attribute_status is in which table? products or product_attributes or is it named just status ? – N69S Apr 01 '22 at 14:13
  • Consider a relation in product model. Take a look [link](https://stackoverflow.com/questions/30231862/laravel-eloquent-has-with-wherehas-what-do-they-mean) – raaahad Apr 01 '22 at 14:14
  • attribute_status is in the Products table – Rahumathulla Apr 01 '22 at 16:15
  • @Rahumathulla If I understand you correctly, you are trying to eager load `ProductAttributes` only when `products.attribute_status ` is `1`? Because you can toggle display product attributes based on a simple `if` condition on the `attribute_status` field. – Huy Phạm Apr 02 '22 at 06:25

2 Answers2

0

Try this:

Product::where('category_id',$request->category_id)
    ->where('products.status',1)
    ->with('productImages', 'productReviews', 'user.vendors', 'subchildcategories', 'ProductAttributes')
    ->get();
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
ekkev
  • 46
  • 4
0
$products = Product::with(['productImages', 'productReviews', 'user.vendors', 'subchildcategories', 'ProductAttributes'])
    ->where('category_id',$request->category_id)
    ->where('products.status',1)
    ->get()
    ->map(function ($product) {
        $product->productAttributes = $product->attribute_status == 1 ? $product->productAttributes : null;

        return $product;
    });
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43