我想按省获取加入项目的计数,并将其名称发送到数组中的视图。

这是我的代码:

public function filterByProvince(Request $request)
{

    $province = [];
    foreach ($request->except(['_token']) as $key => $value) {
        $province[$key] = $key;
    }

    $projects = [];
    foreach ($province as $prov){

        $projects[] = DB::table('places')
            ->join('projects', function ($join) use ($prov) {
                $join->on('places.id', '=', 'projects.place_id')
                    ->where('places.province', '=', $prov);
            })->get();

    }


    return view('admin.projects.province', compact(['projects']));

}

最佳答案

我不清楚您需要什么,下面的内容就足够了吗?

public function filterByProvince(Request $request)
{
    $count = array();
    $province = [];
    foreach ($request->except(['_token']) as $key => $value) {
        $province[$key] = $key;
    }

    $projects = [];
    foreach ($province as $prov){

        $result = DB::table('places')
            ->join('projects', function ($join) use ($prov) {
                $join->on('places.id', '=', 'projects.place_id')
                    ->where('places.province', '=', $prov);
            })->get();
        $projects[] = $result;
        $count[$prov] = count($result);

    }


    return view('admin.projects.province', compact(['projects', 'count']));

}

10-08 03:10