[英]Laravel - use the Eloquent where method from within View
In Laravel 5.4, I have these tables:在 Laravel 5.4 中,我有这些表:
sales_orders : id,order_code, customer_id
out_transactions : id, ref_id
ref_id
refers to the primary key (ie id
) of sales_orders
. ref_id
指的是sales_orders
的主键(即id
)。
First I need to get all the rows from sales_orders
with a particular customer_id
ie 10;首先,我需要从
sales_orders
具有特定customer_id
所有行,即 10; Then for each row, I need to query the out_transactions
table matching its ref_id
against the id
of sales_orders
table.然后对于每一行,我需要查询
out_transactions
表,将其ref_id
与sales_orders
表的id
匹配。
Inside the show_all
method of the controller RequisitionController
, I have :在控制器
RequisitionController
的show_all
方法中,我有:
$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 :在
requisition.blade.php
里面,我有:
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 .但是 sql 显示了意外查询。 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.所以你可以看到
where
子句被连接起来了。 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).并且连接子句会记住
for loop
中ref_id
的第一个、第二个ref_id
以使用它们中的每一个,我猜(我只是无法在查询中打印?
的确切值)。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.