How can I standardize some SQL query in Laravel?

时间:2019-04-17 02:20:08

标签: php laravel-5 standards

I've been working on some SQL query and it works well, but some full stack developer told me it isn't in the best practice, so he doesn't give me any feedback about how can I standardize this one!

This is my model function.

function getExternalSchools( $school_group_id ) {
$schools = DB::connection( 'sqlsrv' )
            ->table( 'T_CLIENTS AS cli' )
            ->join( 'T_SE AS e', 'e.idse', '=', 'cli.idse' )
            ->join( 'T_CITY AS cid', 'cid.cod_city', '=', 'cli.cod_city' )
            ->leftJoin( 'T_CLIENTS_SP AS csp', 'cli.cod_client', '=', 'csp.cod_client', 'AND', 'cli.idse','=','csp.idse' )
            ->leftJoin( 'T_CLIENT_CONTRACT AS cc', 'cc.cod_client', '=', 'cli.cod_client' )
                ->select(                    
                    'cli.cod_client',
                    'cli.Status',
                    'cli.IU'                 
                )
                ->whereRaw(implode(' ', [
                    "cli.category = 'C'",
                    "AND (cli.status = 'A' OR ISNULL(csp.cod_pae,0) <> 0)",
                    "AND cli.idse IN (1, 2, 10)",
                    "AND cli.FisJur = 'J'",
                    "AND cli.ERP_OK <> 0",
                    "AND cli.Typo = ''",
                    "AND cli.Status = 'A'",
                    "AND cli.school_group_id = '{$school_group_id}'",
                ]))
                ->get();

}

This function retrieves some schools from an external SQL SERVER database. So I only can read the data from this database.

Where can I improve this one?

Thank you

0 个答案:

没有答案