как получить все отзывы для каждой модели в цикле ?Laravel
таблицы :
Profiles(id, user_id, name);
Reviews(id, user_id, name, body);
Users(id, name);
модель User имеет связь с Profile один ко одному
есть еще модель Review соответсвенно и таблица в БД в которой каждый отзыв привязывается к User через поле user_id
Вопрос как правильно прокрутить в цикле и получить доступ Reviews
@foreach($profiles as $profile)
как сделать чтобы в $profile->получить (Reviews которые привязаны к user_id такому же как и текущий в цикле $profile ?
@endforeach
class User extends \TCG\Voyager\Models\User
{
public function profile() {
return $this->hasOne(Painter::class);
}
}
class Profile extends Model
{
use HasFactory;
public function user() {
return $this->belongsTo(User::class);
}
public function save(array $options = [])
{
// If no author has been assigned, assign the current user's id as the author of the post
if (!$this->author_id && Auth::user()) {
$this->user_id = Auth::user()->getKey();
}
return parent::save();
}
}
спасибо!
Ответы (1 шт):
Автор решения: Винсентэ Пуххини
→ Ссылка
Вы не указали содержимое класса Review.
Вообще, лично я вижу два пути:
В User добавить связь с Review. Дальше тупо дергать profile->user->review.
Связать Profile с Review через hasOneThrough и получать через profile->review.
public function review() { return $this->hasOneThrough( Review::class, User::class ); }