使用与连接表Mysql laravel 5的区别

时间:2016-09-23 11:24:55

标签: php mysql laravel laravel-5

我的老板有一个任务,使用laravel 5显示来自mysql的数据。 他给我这个查询SQL

SELECT DISTINCT loans.loan_number, borrower.brate, loans.loint, loan_offer.amount,
loans.lterm,SUBSTRING(repayment_lender.repay_due_date,4,2)
FROM loans, loan_offer, borrower, repayment_lender,LENDER
WHERE loans.id = loan_offer.loan_id
AND loans.borrower_id = borrower.id
AND repayment_lender.loans_id = loans.id
AND lender.id = loan_offer.lender_id
AND lender.id = 3

如果执行查询,它将看起来像Like this

我在控制器中使用了这段代码

 $data_loanoffers    =  DB::table('loans')->join('loans', 'loans.id', '=', 'repayment_lender.loans_id')
                                            ->join('loans', 'loans.id', '=', 'loan_offer.loan_id')
                                            ->join('borrower', 'borrower.id', '=', 'loans.borrower_id')
                                            ->join('loan_offer', 'loan_offer.lender_id', '=', 'lender.id')
                                            ->where('lender.id',$l_id)->distinct()->get();
return view('account.investor_portfolio',[
            'data_loanoffers'  => $data_loanoffers,
]);

并在视图中使用此代码来显示数据

 @foreach($data_loanoffers as $loans)       
                                   <tr>
                                        </td>
                                        <td>{{$loans->loan_number}}</div>
                                        <td> {{$borrower->brate}} </div>
                                        <td> {{$loans->loint}} </div>
                                        <td> {{$loan_offer->amount}}</div>
                                        <td> {{$loans->lterm}}</div>
                                        <td> ?? </div>
                                    </tr>

                                    @endforeach

在前端显示错误LIKE THIS

我是laravel 5中的新手。我不知道我的代码错误在哪里。也许高手在这里可以帮助我吗? 提前谢谢。

2 个答案:

答案 0 :(得分:0)

您需要选择一些列,以便可以在唯一字段上应用distinct。

答案 1 :(得分:0)

我只是使用laravel

中的查询构建器来解决我的解决方案
 $data_loanoffers        =  DB::select( DB::raw("SELECT DISTINCT loans.loan_number, borrower.brate, loans.loint, loan_offer.amount,loans.lterm,SUBSTRING(repayment_lender.repay_due_date,4,2) as DueDate
                                    FROM loans, loan_offer, borrower, repayment_lender,LENDER
                                    WHERE loans.id = loan_offer.loan_id
                                    AND loans.borrower_id = borrower.id
                                    AND repayment_lender.loans_id = loans.id
                                    AND lender.id = loan_offer.lender_id
                                    AND lender.id = '$l_id' ") );