mardi 10 juillet 2018

Laravel 5, Join two separate query calls?

Is it possible to join two separate query calls? more specifically is it possible to join $servers and $applications?

$servers = Server::select('servers.*', 'statuses.status as status_name')
->leftJoin('server_statuses', function($join) {
    $join->on('server_statuses.server_id', '=', 'servers.id')
        ->where('server_statuses.id', function($query) {
            $query->select('id')
                ->from('server_statuses')
                ->whereColumn('server_id', 'servers.id')
                ->latest()
                ->limit(1);
        });
})
->leftJoin('statuses', 'statuses.id', '=', 'server_statuses.status_id')
->where('servers.isPublic', '=', 1)
->orderBy('servers.id', 'desc')
->get();

$applications = Apps::join('app_servers', 'apps.id', '=', 'app_servers.app_id')
->get();

The $servers query is executing this sql statement:

select `servers`.*, `statuses`.`status` as `status_name` from `servers` left join `server_statuses` on `server_statuses`.`server_id` = `servers`.`id` and `server_statuses`.`id` = (select `id` from `server_statuses` where `server_id` = `servers`.`id` order by `created_at` desc limit 1) left join `statuses` on `statuses`.`id` = `server_statuses`.`status_id` where `servers`.`isPublic` = '1' order by `servers`.`id` desc

I am trying to avoid having another complicated query. I tried running a union but was getting a binding error, I also tried to do something like below but that didn't give me the results intended.

foreach($servers as $server){
    $applications->add($server);
}

$test = $applications->toarray();

dd($test);

Does anyone have any idea on how I could accomplish this if it is even possible? Or am I going to have to do query on all 5 tables?

(Will post migrations if needed)



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2zxGV3s
via IFTTT

Aucun commentaire:

Enregistrer un commentaire