php - 数组返回 3 维数组而不是二维数组。 PHP/Laravel

标签 php mysql arrays laravel

我当前使用的系统中的二维数组有问题, 所以这是我的问题。 我正在使用 Laravel (如果这是相关信息)并且我有一个名为 StationController 用于名为 Station 的模型。每个有许多摊位,每个摊位有许多人员。 我的问题是如何在不使用 JavaScriptAjax 的情况下将查询结果返回到 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;
}

这是它返回的屏幕截图。

enter image description here

最佳答案

如果模型上的关系设置正确,您可以执行以下操作:

Station::with('booth.personnel')->where('id', $id);

现在“展位”和“人员”需要与模型上的关系方法同名。所以它可能是 Station::with('booths.personnel'),具体取决于您的命名约定。

关于php - 数组返回 3 维数组而不是二维数组。 PHP/Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48918369/

相关文章:

PHP 启动 :Unable to start dynamic library

php - jquery mobile 丢失了带有 ajax post 的 css

php - 访问 php/mysql 查询的结果

c++ - 在 C++ 中对指针数组进行排序

php - 使用带有连字符的 extract()

php - 需要用 PHP 在文件开头写

mysql - 如何为每个 GROUP BY 集返回多个结果?

php - 哪个唯一 ID 用于博客和评论?

java - 从数组中检索特定范围的数据 (Java)

java - 返回二维数组