私は少し失望しています...私はbelongsToとテーブルに参加しようとしていますが、問題があります。多くのbelongsToが同じテーブルにあるLaravel
私はこのテーブルを持っている:パーティー
イド - 日 - ピエロ - makeup1 - makeup2 ...
ピエロ、makeup1、makeup2は外部キー、人々のテーブルへの参照です。
ピープルズテーブル:
イド - 名 - ファーストネーム...
私のシステムではピエロ、makeup1とmakeup2の値は、人々のテーブルのIDです。 これは良いことです。
問題は、ピエロ、メイク1、メイク2の名前を表示したいときです。私は示して私の見解では
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 }}
- >ない仕事と非オブジェクトのプロパティを取得しようとすると
示し
私は理解していない....
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
ありがとうございます。 –
これは私の最初の投稿の一番下にあるdd($ default)です。 – yagrasdemonde
コントローラで実行する 'dd($ defaults)'のようです。ビュー内の同じデータで作業していることを確認し、他のアクションから取得したデータではないことを確認してください。 –