Laravel 有效的运输方式
Laravel active shipping methods
Table:
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()
我是不是做错了什么,或者这样做是不可能的?
你可以使用 laravel scopes 来代替这个:
class Shipping extends Model
{
const YES = 'yes';
const NO = 'no';
public function scopeActive($query)
{
return $query->where('active', '=', self::YES);
}
}
然后
$shipping = Shipping::active()->get();
Table:
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()
我是不是做错了什么,或者这样做是不可能的?
你可以使用 laravel scopes 来代替这个:
class Shipping extends Model
{
const YES = 'yes';
const NO = 'no';
public function scopeActive($query)
{
return $query->where('active', '=', self::YES);
}
}
然后
$shipping = Shipping::active()->get();