Laravel Eloquent(模型属性不会显示)
Laravel Eloquent (A Model attribute won't show)
我有一个 Stat
Class :
class Stat extends Model
{
public static function getByKey($key)
{
return Stat::where("key", "=", $key)->first();
}
public function getDates()
{
return json_decode($this->content);
}
}
在我的数据库中,Table stats
仅包含两列(键、内容)
当我在我的视图中执行此功能时发生错误
{!! \App\Stat::getByKey("ORDERS-DATA")->getDates() !!}
这会导致此错误:
Exception message: Undefined property: App\Stat::$content
所以我将这两行添加到 getDates()
来调试错误:
public function getDates()
{
dd($this->content);//This shows the same error
dd($this->attributes["content"]);//This works fine and show me what I want
return json_decode($this->content);
}
然后我执行了 dd($this)
它显示了这个:
Stat {#486
#connection: "mysql"
#table: "stats"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:5 [
"id" => 8
"key" => "ORDERS-DATA"
"content" => """
[
{
\t"day": "2019-04-01",
\t"count": "5"
}
]
"""
"created_at" => "2019-04-01 14:09:00"
"updated_at" => "2019-04-01 14:11:02"
]
#original: array:5 [
"id" => 8
"key" => "ORDERS-DATA"
"content" => """
[
{
\t"day": "2019-04-01",
\t"count": "5"
}
]
"""
"created_at" => "2019-04-01 14:09:00"
"updated_at" => "2019-04-01 14:11:02"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
}
而我想要的是通过 $this->content
获得我的 content
,但我不明白为什么它不起作用??
请帮忙
发生错误是因为我覆盖了 Laravel 函数之一。
名称 getDates
OR getByKey
OR getCounts
被 Laravel 使用。
This is so weird this error happened to me two times in the same
day, Today's morning I created a function called getAttributes()
and
showed me a total diferrent error. Why the community don't create a
list of functions names that we are not allowed to use, or use more
unique names ???
我有一个 Stat
Class :
class Stat extends Model
{
public static function getByKey($key)
{
return Stat::where("key", "=", $key)->first();
}
public function getDates()
{
return json_decode($this->content);
}
}
在我的数据库中,Table stats
仅包含两列(键、内容)
当我在我的视图中执行此功能时发生错误
{!! \App\Stat::getByKey("ORDERS-DATA")->getDates() !!}
这会导致此错误:
Exception message: Undefined property: App\Stat::$content
所以我将这两行添加到 getDates()
来调试错误:
public function getDates()
{
dd($this->content);//This shows the same error
dd($this->attributes["content"]);//This works fine and show me what I want
return json_decode($this->content);
}
然后我执行了 dd($this)
它显示了这个:
Stat {#486
#connection: "mysql"
#table: "stats"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:5 [
"id" => 8
"key" => "ORDERS-DATA"
"content" => """
[
{
\t"day": "2019-04-01",
\t"count": "5"
}
]
"""
"created_at" => "2019-04-01 14:09:00"
"updated_at" => "2019-04-01 14:11:02"
]
#original: array:5 [
"id" => 8
"key" => "ORDERS-DATA"
"content" => """
[
{
\t"day": "2019-04-01",
\t"count": "5"
}
]
"""
"created_at" => "2019-04-01 14:09:00"
"updated_at" => "2019-04-01 14:11:02"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [
0 => "*"
]
}
而我想要的是通过 $this->content
获得我的 content
,但我不明白为什么它不起作用??
请帮忙
发生错误是因为我覆盖了 Laravel 函数之一。
名称 getDates
OR getByKey
OR getCounts
被 Laravel 使用。
This is so weird this error happened to me two times in the same day, Today's morning I created a function called
getAttributes()
and showed me a total diferrent error. Why the community don't create a list of functions names that we are not allowed to use, or use more unique names ???