简体   繁体   中英

How Can I Use Join In The Leftjoin With Laravel?

I have a question on leftjoin & join in laravel I want to rewrite following sample to laravel, how can I do this ?

LEFT JOIN(advertsolution_f SF 
          JOIN function_d FD ON SF.fd_id = FD.fd_id 
          JOIN function_m FM ON FD.fm_id = FM.fm_id) 
ON SM.ads_id = SF.ads_id 

i try to rewrite it like this,but i think it is not a good idea.

->leftJoin($SF,$SM.'.ads_id',$SF.'.ads_id')
->leftjoin($FD,$SF.'.fd_id',$FD.'.fd_id')
->leftjoin($FM,$FD.'.fm_id',$FM.'.fm_id')

because i have no idea how to join table to $SF privately.

For making such complex query you can use RAW EXPRESSIONS like:

$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->get();

Raw Expressions

Sometimes you may need to use a raw expression in a query. These expressions will be injected into the query as strings, so be careful not to create any SQL injection points! To create a raw expression.

Reference

Map like this,

->leftJoin($SF,$SM.'.ads_id','=',$SF.'.ads_id')
->leftjoin($FD,$SF.'.fd_id','=',$FD.'.fd_id')
->leftjoin($FM,$FD.'.fm_id','=',$FM.'.fm_id')

You are missing = sign in query.

EDIT

EXAMPLE YOU GIVEN, I am writing query on behalf of it, you just need to map with your original fields and tables,

DB::table('a')
->leftJoin("b","b.a_id","=","a.id")
->leftJoin("c","c.b_id","=","b.id")
->leftJoin("d","d.bd_id","=","b.bdid")
->get();

Map this query, as per your requirement, it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM