问题描述
我的根路径如下:
$ user = User :: all();
return $ user-> column-one;
其中返回异常 使用未定义的常量一 - 假设'一' / em> ,即使我的用户表中有一个名为 column-one
的列。那么如何从我的模型中获得 column-one
?
在挖掘雄辩模型的源代码之后,我发现了魔术方法 编辑: I have the following in my root route: Which returns the exception Use of undefined constant one - assumed 'one' even though I do have a column called After digging through the source code for the eloquent model I found the magic method Edit: 这篇关于Laravel:Eloquent如果列名包含破折号,如何获取模型的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! __ get
,并了解到它只是公共函数的包装器 getAttribute
它需要一个字符串,因此我现在可以通过
$ user-> getAttribute('column-one');
。 p>
请参阅@Alexandre Butynski的评论,以获得比我所使用的更好的解决方案。$user = User::all();
return $user->column-one;
column-one
from my users table. So how do I get column-one
from my model?__get
and learned that it was just a wrapper for the public function getAttribute
which takes a string thus I'm now able to retrieve the column via $user->getAttribute('column-one');
.
See @Alexandre Butynski's comment below for a better solution than the one I used.