Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel访问器附加到所有结果_Php_Laravel_Laravel 5.1_Accessor - Fatal编程技术网

Php Laravel访问器附加到所有结果

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

我有一个表字段:

产品 我想要的是得到额外的数据“库存中”。结果将是:

产品 对于单个数据,我可以在控制器本身上修改结果,但在将其转换为多个数据结果时,我会得到堆栈。exp.产品::全部()

我读过关于雄辩的变种人和存取人的文章。我在模型文件上尝试了这种逻辑。但我不知道如何得到结果

这是我的产品型号代码:

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;
}