【发布时间】:2015-02-10 22:49:49
【问题描述】:
我一直在为此自责,似乎无法让我的 JSON 只显示房间的标题和描述(如果它们是实时的)。我试过使用return $this->hasMany('Room')->select(array('id', 'title', 'description'));,但它根本没有返回房间。
有人可以帮忙吗?我确信这一定很容易实现,因为我理解 Laravel Eloquent 对这类任务很简单。
JSON
[
{
id: 1,
building_title: "Drum Castle"
room: [
{
id: 1,
building_id: 7,
title: "Snooker Room",
description: "Full Description",
live: 1
}
]
}
]
建筑模型
public function room()
{
return $this->hasMany('Room');
}
房间模型
public function building()
{
return $this->belongsTo('Building', 'building_id');
}
控制器
$buildings = Building::with('room')->get();
【问题讨论】:
-
Building::with('room')->get();的输出是什么? -
上面的 JSON 是我得到的一个例子。