【发布时间】:2018-12-06 20:05:12
【问题描述】:
我有房间、画廊和图片。我想将画廊与房间相关联,然后我想使用房间模型访问分配的画廊的图像。我是 Laravel 的新手,我查看了 YouTube 课程和文档,但没有找到解决问题的方法。
Room.php:
class Room extends Model
{
protected $table = 'rooms';
public function gallery()
{
return $this->hasOne('App\Gallery');
}
}
图库.php:
class Gallery extends Model
{
protected $table = 'gallery';
public function images()
{
return $this->hasMany('App\Image');
}
public function room()
{
return this->belongsTo('App\Room');
}
}
RoomController.php:
$room = Room::findOrFail($id);
$room_gallery = $room->gallery()->images;
return $room_gallery;
【问题讨论】:
标签: php laravel web web-applications frameworks