【发布时间】:2015-12-18 04:53:17
【问题描述】:
首先我不得不说我试图找到解决方案,但我没有。
基本问题:
$Br = new BrandTop;
dd( $Br->limit(10)->get() ); // Will return 10 rows
和
$Br = new BrandTop;
$Br->limit(10);
dd( $Br->get() ); // Will return all rows.
那么,基本问题 - 为什么?如何为模型设置一些限制,但仍然可以使用它,例如设置(或不设置)一些 where 或 order 取决于其他变量。
高级问题:
我想像这样使用模型:
class BrandTop extends Model
{
public function withBrand() {
return $this->leftJoin('brand', 'brand.id' , '=', 'brandtop.brand_id');
}
public function forType($type) // there is much more conditions for type
{
return $this->where(['type' => $type]);
}
// main function
public function forSunglasses($limit = 0, $logo = false)
{
if ($logo)
$this->where(['menu_logo' => 1])->orderBy('total_sales', 'desc');
if ($limit)
$this->limit($limit);
return $this->forType('sunglasses')->withBrand();
// But there goes Error, because forType() return Builder object, and it has no withBrand() method
}
}
因此,条件要多得多,而且在单独的方法中设置所有条件要容易得多。但是怎么做呢?
【问题讨论】: