jeudi 10 juin 2021

How to make a DB call with an array of ids and get the first row of every of those ids? [duplicate]

I have a php(laravel) code that i try to improve. Right now there are 50 DB calls but i figured out that it's probably possible to do this in 1 DB call but i'm not sure how though. I want to do the DB::Table('app_transaction_infos') query with the array of id's ($loaded_app_ids) and get as a result the first where('app_id', $loaded_app_id) for every id. After that i will run the loop on in, instead of looping the query over and over again. Now i'm not sure though how to write that query for an array as i described.

        foreach($loaded_app_ids as $loaded_app_id){
        if(empty($app_users_30d[$loaded_app_id])){
            // Create array with the app id to show it had 0 data in the last 30 days
            $app_users_30d[$loaded_app_id] = [];

            // Check if any history data of this app is available
            $appTransactionInfo = DB::Table('app_transaction_infos')->where('app_id', $loaded_app_id)->first();
            // If no data at all imported, set to no data
            if(empty($appTransactionInfo)) {
                $nodata[$loaded_app_id] = 'nodata';
            }
            else{
                $nodata[$loaded_app_id] = 'hasdata';
            }
        }
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire