Php Laravel访问器附加到所有结果
我有一个表字段: 产品 我想要的是得到额外的数据“库存中”。结果将是: 产品 对于单个数据,我可以在控制器本身上修改结果,但在将其转换为多个数据结果时,我会得到堆栈。exp.产品::全部()强> 我读过关于雄辩的变种人和存取人的文章。我在模型文件上尝试了这种逻辑。但我不知道如何得到结果 这是我的产品型号代码:Php Laravel访问器附加到所有结果,php,laravel,laravel-5.1,accessor,Php,Laravel,Laravel 5.1,Accessor,我有一个表字段: 产品 我想要的是得到额外的数据“库存中”。结果将是: 产品 对于单个数据,我可以在控制器本身上修改结果,但在将其转换为多个数据结果时,我会得到堆栈。exp.产品::全部() 我读过关于雄辩的变种人和存取人的文章。我在模型文件上尝试了这种逻辑。但我不知道如何得到结果 这是我的产品型号代码: class Product extends Model { protected $appends = ['is_in_stock']; public function getIsInStock
class Product extends Model
{
protected $appends = ['is_in_stock'];
public function getIsInStockAttribute()
{
return $this->attributes['is_in_stock'] = false; // this will be boolean(true/false) result based on the current stock
}
}
请帮助我了解任何线索或参考资料
提前谢谢:)我终于在 因此,我将产品型号代码更改为:
public function getIsInStockAttribute()
{
return ($this->stock > 0) ? true : false;
}
class Product extends Model
{
protected $appends = ['is_in_stock'];
public function getIsInStockAttribute()
{
return $this->attributes['is_in_stock'] = false; // this will be boolean(true/false) result based on the current stock
}
}
public function getIsInStockAttribute()
{
return ($this->stock > 0) ? true : false;
}