2016-12-14 23 views
0

我正在尝试三天将此关系从Laravel query_builder转移到Laravel eqloquent。但它看起来无用:将数据库关系更改为Laravel雄辩

$category_products = DB::table('products') 
      ->Join('categories', 'products.category_id', '=', 'categories.id') 
      ->select('products.*', 'categories.CategoryEnName') 
      ->where('categories.parent_id', '=', '2') 
      ->get(); 

上面的代码中运作良好,但它不是与laravel “分页” 的工作是这样的:

$category_products = DB::table('products') 
       ->Join('categories', 'products.category_id', '=', 'categories.id') 
       ->select('products.*', 'categories.CategoryEnName') 
       ->where('categories.parent_id', '=', '2') 
       ->get()->paginate(15); 

That'smy类模型:

class Category extends Model 
{ 
    protected $guarded = ['id']; 

    //For the nested "tree like" categories 
    public function parent() 
    { 
     return $this->belongsTo('App\Category', 'parent_id'); 
    } 

    //For the nested "tree like" categories 
    public function children() 
    { 
     return $this->hasMany('App\Category', 'parent_id'); 
    } 


    /** 
    * 
    *Grab all the products belong to this category 
    * 
    */ 
    public function products() 
    { 
     return $this->hasMany('App\Product'); 
    } 

我想要的是如何将查询生成器中的代码更改为Laravel雄辩?

回答

1

如果您使用paginate(),则不需要get()