Многие принадлежат тому же столику Laravel

Я немного разочарован … Я пытаюсь объединить свои столы с принадлежащим, но у меня есть проблема.

У меня есть этот стол: party

Id — день — клоун — макияж1 — макияж2 …

клоун, косметика1, косметика2 — внешние ключи, ссылка на таблицу людей.

Народная таблица:

Id — имя — имя …

В моей системе значения clown, косметика1 и макияж2 являются идентификаторами таблицы людей.
Это хорошо.

Проблема в том, когда я хочу показать имя клоуна, косметику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

0

Решение

Я нашел решение!
Чтобы показать отношение, я должен написать:

{{ $defaut->makeup1()->first()->name }}

Спасибо

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector