首先我必须说我试图找到解决方案,但我没有。
基本问题:
$Br = new BrandTop;
dd( $Br->limit(10)->get() ); // Will return 10 rows
Run Code Online (Sandbox Code Playgroud)
和
$Br = new BrandTop;
$Br->limit(10);
dd( $Br->get() ); // Will return all rows.
Run Code Online (Sandbox Code Playgroud)
那么,基本问题是——为什么?如何为模型设置一些限制,但仍然可以使用它,例如设置(或不设置)某些位置或顺序取决于其他变量。
高级问题:
我想像这样使用模型:
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)
{ …
Run Code Online (Sandbox Code Playgroud)