In Laravel 5.4, I have these tables:
sales_orders : id,order_code, customer_id
out_transactions : id, ref_id
ref_id
refers to the primary key (ie id
) of sales_orders
.
First I need to get all the rows from sales_orders
with a particular customer_id
ie 10; Then for each row, I need to query the out_transactions
table matching its ref_id
against the id
of sales_orders
table.
Inside the show_all
method of the controller RequisitionController
, I have :
$query_sales_order=SalesOrder::where('customer_id',10);
$requisitions = $query_sales_order->get();
$model=InventoryOutTransaction::query();
return view('admin.requisition.requisition',compact('requisitions')->withModel($model);
Inside the requisition.blade.php
, I have :
foreach($requisitions as $aP) {
$requisition_id = $aP->id;
$inventory_out_info = $model->where('ref_id', $requisition_id);
echo '<br/> sql = '.$inventory_out_info->toSql();
$inventory_out_result = $inventory_out_info->get();
$inventory_out_info_id = 0;
foreach ($inventory_out_result as $inventory_out_result_indiv) {
$inventory_out_info_id = $inventory_out_result_indiv->id;
echo ' inventory_out_info_id = '.$inventory_out_info_id;
}
}
But the sql shows unexpected query . What I get in a case is
sql = select * from `out_transactions` where `ref_id` = ?
sql = select * from `out_transactions` where `ref_id` = ? and `ref_id` = ?
sql = select * from `out_transactions` where `ref_id` = ? and `ref_id` = ? and `ref_id` = ?
So you can see that the where
clause is being concatenated. And the concatenated clause remembers the 1st, 2nd etc values of ref_id
from the for loop
to use each of them, I guess ( I just cannot print the exact value of ?
in the query).
How can I just remove the concatenation ? or Any other way to achieve the same purpose ?
首先在您的控制器中尝试此查询,并在您的预期结果出现后打印答案,使用此查询更改您的控制器并将结果传递到视图页面并根据您的需要 foreach 结果。
$result = DB::table('sales_orders')->join('out_transactions','sales_orders.id','=','out_transactions.ref_id')->where('sales_orders.customer_id',10)->get();
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.