vendredi 29 juin 2018

Is it possible to run a join on a variable created by a sub-query?

Right now I am running a sub-query to get the most recent status for a server, this sub-query is returning through the variable last_status.

 //This is ran when WithLastStatusDate() is called
$query->addSubSelect('last_status', ServerStatus::select('status_id')
    ->whereRaw('server_id = servers.id')
    ->latest()
);

$servers = Server::WithLastStatusDate()
    ->OrderBy('servers.id', 'desc')
    ->where('servers.isPublic', '=', 1)
    ->get(); 

What I am trying to do now is do a join on this so that it gives me the actual name of the status based on the result of this query in the statuses table. I have tried to do a simple left join but am getting the error that the last_status column isn't found.

$servers = Server::WithLastStatusDate()
    ->OrderBy('servers.id', 'desc')
    ->where('servers.isPublic', '=', 1)
    ->leftjoin('statuses','servers.last_status', '=', 'statuses.id')
    ->get(); 

Can anyone point me in the right direction on how to accomplish this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire