reservations->id - I am getting showtime_id clumn (its ok.)
$reservation->showtime['movie_id'] - I am getting movie_id (its ok.)
But I need movie name.
数据库
- reservations
id
showtime_id
....
- showtimes
id
movie_id
..
- movies
id
movie_name
..
我的观点
@foreach ($reservations as $reservation)
<tr>
<td>{{$reservation->id}}</td>
<td>{{$reservation->fullname}}</td>
<td>{{$reservation->showtime['movie_time']}}</td>
<td>Movie Name</td>
</tr>
@endforeach
我列出了这样的预订。我的控制器
public function Reservations()
{
$reservations = \App\Reservation::paginate(20);
return view('admin.reservations', ['reservations' => $reservations]);
}
我的模态
class Reservation extends Model
{
public function showtime(){
return $this->belongsTo('App\Showtime');
}
}
答案 0 :(得分:4)
在Showtime模型中创建一个关系,以检索与其相关的电影。
// Showtime.php
public function movie()
{
return $this->belongsTo(Movie::class);
}
现在你可以访问它。
$reservation->showtime->movie->movie_name;