Zerontelli
Zerontelli

Reputation: 289

Laravel get products in the same category

Hi im trying to pass all the products in the same category to a view my products table has cat_id which related to cat_id in the categories table, i got all the data from a product id to a single product view and i want to show related products to that product too here my passing to product page's controller

public function product($id){
        $product = Products::where('pro_id',$id)->Paginate(1);
        $products=Products::where('cat_id',$product->cat_id)->get();
        return view('front.product.product', compact('product','products'));
    }

the $product variable works but not the $products variable i get the following error when try to uses $products variable

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$cat_id

and my variables when trying to pass to view

@foreach($product as $show)
<div class="col-md-9">
    <div class="col-md-5 grid">
        <div class="thumb-image"> <img src="{!! url('public/backend/images/products/'.$show->pro_img) !!}" data-imagezoom="true" class="img-responsive" style="width: 305px; height: 400px;"> </div>
    </div>
<div class="col-md-7 single-top-in">
                        <div class="single-para simpleCart_shelfItem">
                            <h1>{{$show->pro_title}}</h1>
                            <p>{{$show->pro_descript}}</p>

                                <label  class="add-to item_price">{{$show->pro_price}}</label>

                            <div class="available">
                                <h6>Description</h6>
                                <h4>{{$show->pro_detail}}</h4>
                        </div>
                                <a href="#" class="cart item_add">Add to cart</a>
                        </div>
                    </div>
            <div class="clearfix"> </div>
            <div class="content-top1">
            <div class="clearfix"> </div>
            </div>
            @foreach($products as $related)
                            <div class="col-md-4 col-md3">
                                <div class="col-md1 simpleCart_shelfItem">
                                    <a href="single.html">
                                        <img class="img-responsive" src="{!! url('public/backend/images/products/'.$related->pro_img) !!}" alt="" />
                                    </a>
                                    <h3><a href="single.html">{{$related->pro_detail}}</a></h3>
                                    <div class="price">
                                            <h5 class="item_price">{{$related->pro_price}}</h5>
                                            <a href="#" class="item_add">Add To Cart</a>
                                            <div class="clearfix"> </div>
                                    </div>
                                </div>
                            </div>
            @endforeach
</div>
@endforeach

when i dd($product) with paginate(1)

LengthAwarePaginator {#237 ▼
  #total: 1
  #lastPage: 1
  #items: Collection {#226 ▼
    #items: array:1 [▼
      0 => Products {#232 ▼
        #table: "tbl_products"
        #primaryKey: "pro_id"
        +timestamp: false
        #filltable: array:9 [▼
          0 => "cat_id"
          1 => "pro_title"
          2 => "pro_descript"
          3 => "pro_detail"
          4 => "pro_img"
          5 => "quantity"
          6 => "pro_date"
          7 => "pro_price"
          8 => "pro_status"
        ]
        #connection: "mysql"
        #keyType: "int"
        +incrementing: true
        #with: []
        #withCount: []
        #perPage: 15
        +exists: true
        +wasRecentlyCreated: false
        #attributes: array:12 [▼
          "pro_id" => 2
          "cat_id" => 9
          "pro_title" => "veston "
          "pro_descript" => "look good"
          "pro_detail" => "blue jean"
          "pro_img" => "1516593265.jpg"
          "quantity" => 8
          "pro_date" => "2018-01-22 10:54:25"
          "pro_price" => 400.0
          "pro_status" => 0
          "created_at" => "2018-01-22 03:54:25"
          "updated_at" => "2018-01-22 03:54:25"
        ]
        #original: array:12 [▼
          "pro_id" => 2
          "cat_id" => 9
          "pro_title" => "veston "
          "pro_descript" => "look good"
          "pro_detail" => "blue jean"
          "pro_img" => "1516593265.jpg"
          "quantity" => 8
          "pro_date" => "2018-01-22 10:54:25"
          "pro_price" => 400.0
          "pro_status" => 0
          "created_at" => "2018-01-22 03:54:25"
          "updated_at" => "2018-01-22 03:54:25"
        ]
        #changes: []
        #casts: []
        #dates: []
        #dateFormat: null
        #appends: []
        #dispatchesEvents: []
        #observables: []
        #relations: []
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #fillable: []
        #guarded: array:1 [▼
          0 => "*"
        ]
      }
    ]
  }
  #perPage: 1
  #currentPage: 1
  #path: "http://localhost:81/shop/product/2"
  #query: []
  #fragment: null
  #pageName: "page"
}

Upvotes: 2

Views: 2139

Answers (2)

Etibar
Etibar

Reputation: 578

$product = Products::where('pro_id',$id)->Paginate(1);

this code returns array cahnge code to like below:

$product = Products::where('pro_id',$id)->first();

Upvotes: 2

Nick
Nick

Reputation: 766

Here you are calling a paginator:

$product = Products::where('pro_id',$id)->Paginate(1);

Try this instead:

$product = Products::where('pro_id',$id)->first();

-or-

$product = Products::where('pro_id',$id)->paginate(1)[0];

-or finally-

$product = Product::findOrFail($id);

The latter would only work if you've set protected $primaryKey = 'pro_id'; within your class.

The details:

When you called $product = Products::where('pro_id',$id)->Paginate(1);' you are getting aPaginatorinstance back from the data layer. ThePaginatordoesn't have the properties that aProduct` does in your example, which is where the error is coming from.

A paginator is a different type of collection, but it is a type of collection, also. Think about it like an array or like a Java style ArrayList which is a complex wrapper class for an array that provides additional funcitonality. A paginator is similar, but with an extra level of abstraction to provide view based pagination. You can access objects on the 'page' within the paginator similarly to how you can get array-like access to objects in a collection.

If you just return a collection, then doing

$product = Products::where('pro_id',$id)->get();

Gives you the the matching id as a collection. In order to actually see the object you want you would need to do this:

$product = Products::where('pro_id',$id)->paginate(1)[0];

to get the first item in that collection.

Upvotes: 0

Related Questions