小编Lev*_*sha的帖子

在 Laravel 5.1 中将查询生成器条件设置为模型

首先我必须说我试图找到解决方案,但我没有。

基本问题:

$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)

php model laravel

2
推荐指数
1
解决办法
1921
查看次数

标签 统计

laravel ×1

model ×1

php ×1