如何解决Laravel主动运输方式
表格:
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();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。