Home > database >  Query results in laravel
Query results in laravel

Time:04-25

I an new in laravel, and I want to debug a problem that I have. I want to see the query to the DB.

The problem is that in the print it is not displayed as query but as structure.

$query = DealDsp::select('deal_dsp.dsp_id', 'dsp.name_display')
                    ->join('dsp', 'dsp.id', '=', 'deal_dsp.dsp_id')
                    ->join('deal', 'deal.deal_id', '=', 'deal_dsp.deal_id')
                    ->orderBy('dsp.name_display');
                    \Log::info('The query in line 106 is: ');  
                    dd($query);  

I just want to know in the end the $query will be with only tow columns? Or since exists join it contains all the fields from DealDsp table dsp tabl deal table?

Is there a way to print the query as SQL statement?

CodePudding user response:

Put the select after joins and it's better to declare every query name using AS.

$query = DealDsp::join('dsp', 'dsp.id', '=', 'deal_dsp.dsp_id')
                    ->join('deal', 'deal.deal_id', '=', 'deal_dsp.deal_id')
                    ->select([
                    'deal_dsp.dsp_id AS id', 
                    'dsp.name_display AS name'
                    ])
                    ->orderBy('dsp.name_display', 'ASC')
                    ->get();
                    \Log::info('The query in line 106 is: ');

CodePudding user response:

If i get you, you want to print the mysql query of this elequent

you can do this by adding ->toSql() at the end of you statement

        $query = DealDsp::select('deal_dsp.dsp_id', 'dsp.name_display')
                ->join('dsp', 'dsp.id', '=', 'deal_dsp.dsp_id')
                ->join('deal', 'deal.deal_id', '=', 'deal_dsp.deal_id')
                ->orderBy('dsp.name_display')
                 ->toSql();
           dd($query);  

CodePudding user response:

I guess you want to see the SQL query. Your $query is actually "Query Builder". Can you try this:

$sql = $query->toSql(); 
dd($sql);
  • Related