Laravel获得同类产品

时间:2018-01-22 15:46:57

标签: laravel controller eloquent many-to-many

您好我试图将同一类别中的所有产品传递给视图 我的products表有cat_id,它与类别表中的cat_id有关, 我将产品ID中的所有数据都收集到单个产品视图中,并且我也希望向该产品显示相关产品 在这里我传递给产品页面的控制器

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'));
    }

$ product变量有效 但不是$ products变量 尝试使用$ products变量

时出现以下错误
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$cat_id

和我尝试传递给视图时的变量

@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

当我dd($ product)与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"
}

2 个答案:

答案 0 :(得分:2)

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

此代码返回数组 cahnge代码:

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

答案 1 :(得分:0)

在这里你要打电话给一个分页器:

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

请改为尝试:

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

-OR -

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

- 或者最后 -

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

后者只有在您的班级中设置protected $primaryKey = 'pro_id';时才有效。

细节:

当你在你的例子中调用$product = Products::where('pro_id',$id)->Paginate(1);' you are getting a Paginator instance back from the data layer. The Paginator doesn't have the properties that a Product`时,这就是错误发生的地方。

paginator是一种不同类型的集合,但它也是一种集合。可以把它想象成一个数组,或者像Java样式ArrayList,它是一个提供额外功能的数组的复杂包装类。分页器类似,但具有额外的抽象级别以提供基于视图的分页。您可以访问&#39;页面上的对象。在paginator中类似于如何获得对集合中对象的类似数组的访问。

如果您只是返回一个集合,那么

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

将匹配的ID作为集合提供给您。为了真正看到你想要的对象,你需要这样做:

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

获取该集合中的第一项。