hengsopheak February 2016

How to used select join with specify any fields in laravel5

I want to to select from 3 table with Join in Laravel5 but it the first times for me

public function getJD($id){

    $result = [];
    $result = JournalDetail::select('journal_detail.*, jdId as journal_detail, jdJ_id as journal_detail.journal_id .............,tranId as transactions_requiry,..............')
        ->join('journal_requiry','journal_requiry.id','=','journal_detail.journal_id')
        ->leftJoin('transactions_requiry','transactions_requiry.id','=','journal_requiry.tran_id')
        ->where('journal_detail.journal_id','=',$id)->get();
    return $result;

}

Errors,

QueryException in Connection.php line 624: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as `as` from `tb_journal_detail` inner join `tb_journal_requiry` on `tb_journal_' at line 1 (SQL: select `tb_journal_detail`.* as `as` from `tb_journal_detail` inner join `tb_journal_requiry` on `tb_journal_requiry`.`id` = `tb_journal_detail`.`journal_id` left join `tb_transactions_requiry` on `tb_transactions_requiry`.`id` = `tb_journal_requiry`.`tran_id` where `tb_journal_detail`.`journal_id` = 26)

But I can't do this I don't know how to do in Laravel5. Please help

Answers


Gouda Elalfy February 2016

put DB::raw( after select so your query will be:

$result = JournalDetail::select(DB::raw('journal_detail.*, jdId as journal_detail, jdJ_id as journal_detail.journal_id .............,tranId as transactions_requiry,..............'))
        ->join('journal_requiry','journal_requiry.id','=','journal_detail.journal_id')
        ->leftJoin('transactions_requiry','transactions_requiry.id','=','journal_requiry.tran_id')
        ->where('journal_detail.journal_id','=',$id)->get();

Post Status

Asked in February 2016
Viewed 1,884 times
Voted 10
Answered 1 times

Search




Leave an answer