我当前使用的系统中的二维数组有问题, 所以这是我的问题。 我正在使用 Laravel (如果这是相关信息)并且我有一个名为 StationController 用于名为 Station 的模型。每个站有许多摊位,每个摊位有许多人员。 我的问题是如何在不使用 JavaScript 或 Ajax 的情况下将查询结果返回到 View 。
这是 View 的结构:
Station id: 1
- Booth id: 1
- Personnel id: 1
- Personnel id: 2
- Personnel id: 3
- Booth id : 2
- Personnel id: 4
- Personnel id: 5
- Personnel id: 6
Station id : 2
...
这是我在该 Controller 内的函数,但它返回三维数组而不是 2。
public function show($id){
$stations = stations::getstationWhere($id); // select current station
$station = $stations[0]; // select fields of the current station.
$services = stationsServices::getstationsServicesWhere($id); // services of the current station.
$service_booth = ServiceBooths::getServiceBoothBystation($id);
foreach($service_booth as $key => $value){
$booth_personnel[$key][$value->name] = BoothPersonnel::getBoothPersonnel($value->id);
}
return $booth_personnel;
}
这是它返回的屏幕截图。
最佳答案
如果模型上的关系设置正确,您可以执行以下操作:
Station::with('booth.personnel')->where('id', $id);
现在“展位”和“人员”需要与模型上的关系方法同名。所以它可能是 Station::with('booths.personnel')
,具体取决于您的命名约定。
关于php - 数组返回 3 维数组而不是二维数组。 PHP/Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48918369/