[英]Nested SQL Query in Laravel Controller
I have two queries running in my controller. 我在控制器中运行了两个查询。 I need a value from the first query to be passed into the second.
我需要将第一个查询的值传递到第二个查询。 I want the result of both these queries sent to my view.
我希望将这两个查询的结果发送到我的视图。
public function jobs()
{
$query = DB::table("dbQuotes")
->leftJoin("dbACT", "dbQuotes.act_id", "=", "dbACT.ID")
->leftJoin("dbOpps", "dbQuotes.act_id", "=", "dbOpps.contactID")
->leftjoin('dbBids', 'dbQuotes.act_id','=',
DB::raw('dbBids.quote_id AND dbBids.user_id = '. Auth::user()->id))
->where("dbQuotes.active", "=", "1")
->select("dbQuotes.*", "dbACT.*", "dbBids.*",
(DB::raw('date_format(dbQuotes.posted_date, "%d/%m/%Y %H:%i") as posted_date')),
(DB::raw('date_format(dbOpps.expected_date, "%d/%m/%Y") as expected_date')))
->groupBy("dbQuotes.id")
->orderBy("posted_date", "desc")
->get();
$passinvaluehere = $query->dbQuotes.act_id
$bids = DB::table("dbBids")
->where("quote_id", "=", $passinvaluehere)
->get();
return view('jobs', ['query' => $query,'bids' => $bids]);
}
My query works and the view is established in the correct way if I replace the passed value with a number, ie "8763". 如果我将传递的值替换为数字,即“ 8763”,则我的查询有效,并以正确的方式建立了视图。 My question is how, within this function, can I pass the value/s of dbQuotes.act_id into this second query?
我的问题是,如何在此函数中将dbQuotes.act_id的值传递给第二个查询?
***UPDATED Code from answer: [error Call to a member function lists() on a non-object] ***答案中的更新代码:[错误调用非对象上的成员函数lists()]
public function jobs()
{
$query = DB::table("dbQuotes")
->leftJoin("dbACT", "dbQuotes.act_id", "=", "dbACT.ID")
->leftJoin("dbOpps", "dbQuotes.act_id", "=", "dbOpps.contactID")
->leftJoin('dbBids', 'dbQuotes.act_id','=',
DB::raw('dbBids.quote_id AND dbBids.user_id = '. Auth::user()->id))
->where("dbQuotes.active", "=", "1")
->select("dbQuotes.*", "dbACT.*", "dbBids.*",
(DB::raw('date_format(dbQuotes.posted_date, "%d/%m/%Y %H:%i") as posted_date')),
(DB::raw('date_format(dbOpps.expected_date, "%d/%m/%Y") as expected_date')))
->groupBy("dbQuotes.id")
->orderBy("posted_date", "desc")
->get();
$act_id = $query->lists('act_id');
$bids = DB::table("dbBids")
->whereIn("quote_id", $act_id)
->get();
return view('jobs', ['query' => $query,'bids' => $bids]);
}
If you have multiple records (as per the ->get()
method) you have two ways: either you loop over the Collection and make a query each iteration (bad) or you create an array of ids and use a whereIn
in the second query (better): 如果您有多个记录(按照
->get()
方法),则有两种方法:要么遍历Collection并在每次迭代中进行查询(错误),要么创建一个ID数组并在第二个中使用whereIn
查询(更好):
$passinvaluehere = $query->lists('act_id');
// https://laravel.com/docs/5.2/queries#retrieving-results
// this creates and array of `act_id` s
$bids = DB::table("dbBids")
->whereIn("quote_id", $passinvaluehere)
->get();
// you now have a Collection of multiple $bids
If you expect only a single records from your first query, you need to change the fetcher method, using first()
instead, or else take only the first element of your actual collection, something like first($query)
or $query[0]
如果您只希望从第一个查询中获得一条记录,则需要使用
first()
来更改fetcher方法,或者仅获取实际集合的第一个元素,例如first($query)
或$query[0]
$query = DB::table("dbQuotes")
....
->first();
$passedvaluehere = $query->act_id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.