nam_co
2 years ago
121
7
Laravel

Trying to put a key/value in a collection

Posted 2 years ago by nam_co

Hi, hope somebody can help me, Im trying to put a key (city name) inside the projects results, I tried join, but since cities is in a external DB I couldn't , I tried collection push and map, but Im just not that good

        $projects = DB::table('projects')->get();
        $cities = DB::connection('mysql2')->table('cities')->select('geoid','name')->get();
        
        foreach($projects as $project) {
            $name = $cities->where('geoid', $project->city_id)->pluck('name')->first();
            $project['city'] = $name;
        }

Appreciate any help Regards

Please sign in or create an account to participate in this conversation.