用join连接一个Eloquent查询,其中和orderBy-laravel 4

时间:2014-08-22 09:36:06

标签: php laravel orm laravel-4 eloquent

我有以下ORM

$trucksobj = DB::Table('trucks')
             ->orderBy('trucks.id','desc')
             ->join('trucktypes', 'trucks.trucktype_id', '=', 'trucktypes.id');

if($overweight=="on") 
        $trucksobj->where('trucktypes.max_weight', '>', 'trucks.weight');

$trucks=$trucksobj->get();

它就像一个魅力,但是当我用get()替换paginate()时,它没有响应,即

$trucks=$trucksobj->paginate(5);

无效?

我错过了什么?

2 个答案:

答案 0 :(得分:0)

请遵循:http://culttt.com/2014/02/24/working-pagination-laravel-4/

使用手动查询时请使用分页器功能:

$trucksobj = DB::Table('trucks')
             ->orderBy('trucks.id','desc')
             ->join('trucktypes', 'trucks.trucktype_id', '=', 'trucktypes.id');

if($overweight=="on") 
        $trucksobj->where('trucktypes.max_weight', '>', 'trucks.weight');

$trucks=$trucksobj->get();

//$paginator = Paginator::make($items, $totalItems, $perPage);
$paginator = Paginator::make($trucks, count($trucks), 5);

答案 1 :(得分:0)

 public function pagination()
 {
    $users = DB::table('img')
        ->join('category', 'category.id', '=', 'img.category_id')
        ->select('img.category_id', 'category.name', 'img.img_url')
        ->paginate(2);

    return view('page', compact('users'));
 }