Home > Blockchain >  show relation table based on the parent table column in laravel
show relation table based on the parent table column in laravel

Time:04-02

I want to show the the ProductAttributes relation based on the product 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()

CodePudding user response:

Try this:

Product::where('category_id',$request->category_id)
->where('products.status',1)
->with('productImages','productReviews','user.vendors','subchildcategories','ProductAttributes')
->get();

CodePudding user response:

$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;
    });
  • Related