我是laravel的新手,所以我正在寻找如何将特定的id发送到控制器以便从另一个表中获取数据?
例如
while($shipment = sqlsrv_fetch_array($get_customer_rule,SQLSRV_FETCH_ASSOC)) {
//display some data ......
// inside the loop i will have another query to get data from another table has //relation with shipment table
$id = $shipment['id'];
$customer = "SELECT * FROM [ccctadm].[customer] WHERE id = '$id' ";
$get_customer_info = sqlsrv_query($conn, $customer);
$get_customer_id = sqlsrv_fetch_array($get_customer_info,SQLSRV_FETCH_ASSOC);
$customer_id = $get_customer_id['customerid'];
}
我无法在laravel中的while循环中编写查询,因此如何将货件ID传递给控制器以便我可以获取与货件相关的客户数据
答案 0 :(得分:2)
由于你是Laravel的新手,也许你应该首先学习Laravel方法。观看此视频,了解如何使用Eloquent以及该系列中的所有其他视频。 https://laracasts.com/series/laravel-from-scratch-2017/episodes/7
一旦了解了这一点,您就可以将查询重写为
$shipments = Shipement::all();
foreach( $shipments as $shipment ) {
$customer = $shipment->customer;
$customer_id = $customer->id;
}
当你进一步使用laravel并且能够使用急切加载时,你会做得更好
$shipments = Shipment::with('customer')->get();
在你看来
@foreach($shipments as $shipment)
Customer ID is : {{ $shipment->customer->id }}
@endforeach
您决定与Laravel合作。利用它。它将使一切变得更容易,并加快您的开发过程。
如果您想坚持原始SQL查询,可以使用查询构建器
$result = DB::select("SELECT * FROM [ccctadm].[customer] WHERE id = ?", [$id]);
使用结果