Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 调用关系时的即时加载_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 调用关系时的即时加载

Php 调用关系时的即时加载,php,laravel,laravel-5,Php,Laravel,Laravel 5,我的数据库中有这些表,它们是相关的 category products product_images product_categories 在类别模型中我有(请忽略语法错误) 在我的ProductCategory模型中 function Product(){ return $this->belongsTo('App\Product')->where('hidden' , 0 ); } function Image(){ return

我的数据库中有这些表,它们是相关的

category
products
product_images
product_categories 
在类别模型中我有(请忽略语法错误)

在我的ProductCategory模型中

   function Product(){
       return $this->belongsTo('App\Product')->where('hidden' , 0 );
   }
   function Image(){
       return $this->hasMany('App\Image');
   }
在产品模型中,我有

   function Product(){
       return $this->belongsTo('App\Product')->where('hidden' , 0 );
   }
   function Image(){
       return $this->hasMany('App\Image');
   }
我想在我的索引中显示每个类别的最后6个产品 所以我读取并将类别从控制器发送到我的视图

$categories = Category::all();
return view('index' , compact('category'));
在我看来,我有点像

@foreach($categories as $category)

     {{$category->title }}  products :

     @foreach($category->ProductCategoryLatest as $product_category )

             {{$product_category->Product->title }}

             @foreach($product_category->Product->Image as $image  )
                 <img src="{{$image->title}}">
             @endforeach


     @endforeach

@endforeach
select * from product_categories  where category_id  in (1,2,3,4,5) LIMIT 6 
@foreach($categories as $category)


    @foreach($category->ProductCategoryLatest->with('Product' , 'Product.Image' ) as $product_category )

    @endforeach


@endforeach
我的数据库中有5个类别,查询如下

@foreach($categories as $category)

     {{$category->title }}  products :

     @foreach($category->ProductCategoryLatest as $product_category )

             {{$product_category->Product->title }}

             @foreach($product_category->Product->Image as $image  )
                 <img src="{{$image->title}}">
             @endforeach


     @endforeach

@endforeach
select * from product_categories  where category_id  in (1,2,3,4,5) LIMIT 6 
@foreach($categories as $category)


    @foreach($category->ProductCategoryLatest->with('Product' , 'Product.Image' ) as $product_category )

    @endforeach


@endforeach
因此,如果id为1的类别在product_类别中有大约10行,那么所有返回的6行都将属于category 1,其余的返回为空,即使它们在DB中有关联的行,但是限制6会导致它们在product_类别中没有任何行

所以基本上我需要在关系的视图中加载,比如

@foreach($categories as $category)

     {{$category->title }}  products :

     @foreach($category->ProductCategoryLatest as $product_category )

             {{$product_category->Product->title }}

             @foreach($product_category->Product->Image as $image  )
                 <img src="{{$image->title}}">
             @endforeach


     @endforeach

@endforeach
select * from product_categories  where category_id  in (1,2,3,4,5) LIMIT 6 
@foreach($categories as $category)


    @foreach($category->ProductCategoryLatest->with('Product' , 'Product.Image' ) as $product_category )

    @endforeach


@endforeach
顺便说一句,我知道我可以做一些杂耍在控制器像

$new_categories = [] ;
foreach ($categories as $category )
{
    $ProductCategoryLatest  = ProductCategoryLatest::where('category_id' , $category->id )->with('Product' , 'Product.Image')->get();
    $category->ProductCategoryLatest = $ProductCategoryLatest ;
    $new_categories[] = $category ;
}
但我想知道是否有更干净的解决方案


总结一下我的关系

class Category extends Model { 
    function ProductCategoryLatest()
    {
        return $this->hasMany('App\ProductCategory')->limit(6);
    }
}
关于这个代码

$categories = Category::with('ProductCategoryLatest')->get();
将生成此查询

select * from product_categories  where category_id  in (1,2,3,4,5) LIMIT 6 
但我希望每个类别有6行,而不是像这样的所有类别都有6行

category_id= 1 -> 6 row 
category_id= 2 -> 6 row
category_id= 3 -> 6 row
..

好,您可以使用
take()
skip()

好的,您可以使用
take()
skip()


如果你得到结果,你可以接受并向上投票我的答案>如果你得到结果,你可以接受并向上投票我的答案>抱歉,项目暂停了一段时间,这不起作用,我编辑了我的问题,并在最后添加了一个总结,我认为这让一切都变得清晰了。你的回答似乎对
类别
选择设置了6个限制,而不是
ProductCategoryList
,不是吗?是的。它会限制类别我想你需要限制一下吗?抱歉,项目暂停了一段时间,这不起作用,我编辑了我的问题,并在最后添加了一个总结,我认为这让一切都变得清晰了。你的回答似乎对
类别
选择设置了6个限制,而不是
ProductCategoryLast
,不是吗?是的。它会限制类别我想你需要限制它?