0

桌子:

       Schema::create('shippings', function (Blueprint $table) {
            $table->id();
            $table->string('name',64);
            $table->integer('price');
            $table->enum('active', ['yes','no'])->default('yes');
            $table->timestamps();
        });
    }

模型:

class Shipping extends Model
{
    const YES = 'yes';
    const NO = 'no';

    public function isActive()
    {
        return $this->active == self::YES;
    }
}

我想通过使用这样的模型功能只显示活跃的

 $shipping = Shipping::with('isActive')->get();

但后来我得到

错误 调用 bool 上的成员函数 addEagerConstraints()

我做错了什么还是不可能以这种方式做到这一点?

4

1 回答 1

0

Instead of this you can use laravel scopes Like :

class Shipping extends Model
{
    const YES = 'yes';
    const NO = 'no';

    public function scopeActive($query)
    {
        return $query->where('active', '=', self::YES);
    }
}

And then

$shipping = Shipping::active()->get();
于 2020-08-27T12:23:26.097 回答