Laravel orderBy不存在

时间:2018-10-11 15:01:48

标签: laravel

我正在做简单的查询

  $offers = Offer::join('offer_speciality', 'offers.id', 'offer_speciality.offer_id')
            ->join('specialities', 'offer_speciality.speciality_id', 'specialities.id')
            ->when($speciality, function($query, $speciality) {
                 return $query->where('specialities.number', $speciality)->get();
            })
            ->orderBy('created_at','desc')->paginate(9);

然后出现错误:

  

方法Illuminate \ Database \ Eloquent \ Collection :: orderBy不   存在

我不确定这是什么吗?

1 个答案:

答案 0 :(得分:2)

在运行->get()时,将返回一个集合,而->orderBy()不是可以在该集合上运行的函数,并且该文档中列出了所有可用的函数。

您的->get()之前是->orderBy(),这是行不通的。另外,您不需要->get(),就->paginate(9)

您需要:

$offers = Offer::join('offer_speciality', 'offers.id', 'offer_speciality.offer_id')
      ->join('specialities', 'offer_speciality.speciality_id', '=', 'specialities.id')
      ->when($speciality, function($query, $speciality) {
           return $query->where('specialities.number', $speciality)
      })
      ->orderBy('created_at','desc')->paginate(9)