SQL queries are not one of my strong suits, and I have run into a problem that I was able to solve but am hoping to improve and make more efficient. I am using Laravel's Query Builder in these examples but don't mind using a raw request if I have to.
Currently I am making two queries as follows and merging them after.
$acc_id = 10;
$locat_id= 3;
//First get all sites with account id
$allSites = collect(DB::table('sites')->where('acc_id', $acc_id)->get()):
//Next get only sites with account id and connections with current location id
$connectedSites = collect(
DB::table('sites')
->select('sites.id','name','active','url')
->join('connections as conn','sites.id','=','conn.site_id')
->where('acc_id',$acc_id)
->where('conn.locat_id',$locat_id)
->get()
);
//Merge the collections and drop first row with duplicate site_id
$sites = $allSites->merge($connectedSites)->keyBy('id');
return $sites;
So this gives me the desired results. E.g. all the sites that are associated with the account id, and also the connection data for sites associated with both the account and location ids. However, I would like to learn how to do this in one query if possible.