许多 belongsTo 在同一个 table Laravel
Many belongsTo on the same table Laravel
我有点失望...我正在尝试使用 belongsTo 加入我的 table,但我遇到了问题。
我有这个 table : 派对
id -- day -- clown -- makeup1 -- makeup2 ...
clown、makeup1、makeup2为外键,参考人物table。
人民table:
身份证--姓名--名字...
在我的系统中,clown、makeup1 和 makeup2 的值是人的 ID table。
这个不错。
问题是当我想显示小丑、makeup1 和 makeup2 的名字时。
我有错误
Trying to get property of non-object
在我的模型中,我这样定义我的关系:
public function people()
{
return $this->belongsTo(People::class, 'clown', 'id');
}
public function makeup1()
{
return $this->belongsTo(People::class, 'makeup1', 'id');
}
public function makeup2()
{
return $this->belongsTo(People::class, 'makeup2', 'id');
}
在我看来,我是这样展示小丑的:
{{ $defaut->people->name }}
一个有效,另一个无效。
{{ $defaut-> makeup1->name }}
{{ $defaut-> makeup2->name }}
-> 不工作并显示
Trying to get property of non-object
没看懂....
PS :我在控制器中的请求是
$defaults = Default::with('people')->with('makeup1')->with('makeup2')->get();
这个有效,我在 var_dump() 中看到了关系。
<pre>Collection {#654 ▼
#items: array:1 [▼
0 => Default {#619 ▼
#table: "defaults"
#fillable: array:8 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:11 [▶]
#original: array:11 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:3 [▼
"people" => People {#655 ▶}
"makeup1" => People {#687 ▶}
"makeup2" => People {#719 ▼
#fillable: array:7 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▶]
#original: array:10 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
</pre>
Thank you for your advices
我找到了解决方案!
为了显示我必须写的关系:
{{ $defaut->makeup1()->first()->name }}
谢谢
我有点失望...我正在尝试使用 belongsTo 加入我的 table,但我遇到了问题。
我有这个 table : 派对
id -- day -- clown -- makeup1 -- makeup2 ...
clown、makeup1、makeup2为外键,参考人物table。
人民table:
身份证--姓名--名字...
在我的系统中,clown、makeup1 和 makeup2 的值是人的 ID table。 这个不错。
问题是当我想显示小丑、makeup1 和 makeup2 的名字时。
我有错误
Trying to get property of non-object
在我的模型中,我这样定义我的关系:
public function people()
{
return $this->belongsTo(People::class, 'clown', 'id');
}
public function makeup1()
{
return $this->belongsTo(People::class, 'makeup1', 'id');
}
public function makeup2()
{
return $this->belongsTo(People::class, 'makeup2', 'id');
}
在我看来,我是这样展示小丑的:
{{ $defaut->people->name }}
一个有效,另一个无效。
{{ $defaut-> makeup1->name }}
{{ $defaut-> makeup2->name }}
-> 不工作并显示
Trying to get property of non-object
没看懂....
PS :我在控制器中的请求是
$defaults = Default::with('people')->with('makeup1')->with('makeup2')->get();
这个有效,我在 var_dump() 中看到了关系。
<pre>Collection {#654 ▼
#items: array:1 [▼
0 => Default {#619 ▼
#table: "defaults"
#fillable: array:8 [▶]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:11 [▶]
#original: array:11 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:3 [▼
"people" => People {#655 ▶}
"makeup1" => People {#687 ▶}
"makeup2" => People {#719 ▼
#fillable: array:7 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▶]
#original: array:10 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
</pre>
Thank you for your advices
我找到了解决方案! 为了显示我必须写的关系:
{{ $defaut->makeup1()->first()->name }}
谢谢