我不完全确定问题是什么,因为我不理解我收到的错误信息。
我的车辆型号上有以下关系
/**
* The dealer the vehicle belongs to (one-to-one relationship)
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function dealer()
{
return $this->belongsTo('App\Dealer');
}
/**
* The vehicle's specifications
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function specifications()
{
return $this->hasMany('App\Specification');
}
如果我在$ request中有“省”时调用下面的方法,我会收到以下错误:App \ Project \ Frontend \ Repo \ Vehicle \ EloquentVehicle :: App \ Project \ Frontend \ Repo \缺少参数2车辆{闭合}()
当请求中有“城市”时,我也会收到错误消息。
public function vehicleSearchResults($request)
{
$allowedInput = array(
'year',
'make',
'model',
'city',
'province',
'price',
'Vehicle Type'
);
$query = $this->vehicle;
foreach($allowedInput as $value)
{
if($request->has($value))
{
switch ($value)
{
case 'year':
$query = $query->where('model_year', $request->input($value));
break;
case 'make':
$query = $query->where('make', $request->input($value));
break;
case 'model':
$query = $query->where('model', $request->input($value));
break;
case 'price':
$query = $query->where('price', $request->input($value));
break;
case 'city':
$query = $query->whereHas('dealer', function($subQuery, $request, $value)
{
$subQuery->where('city', $request->input($value))
->where('province', $request->input('province'));
})->with('dealer');
break;
case 'province':
$query = $query->whereHas('dealer', function($subQuery, $request, $value)
{
$subQuery->where('province', $request->input($value));
})->with('dealer');
break;
case 'Vehicle Type':
$query = $query->whereHas('specifications', function($subQuery, $request, $value)
{
$subQuery->where('spec', $value)
->where('spec_value', $request->input($value));
})->with('specifications');
break;
}
}
}
return $query->paginate(10);
}
我不知道错误意味着什么,但它指向省开关案例中的这段代码行。
$query = $query->whereHas('dealer', function($subQuery, $request, $value)
答案 0 :(得分:1)
$request
和$value
变量包含您需要在子查询中使用的信息,以及您尝试执行此操作的方式是错误的。您正在创建新变量而不是使用现有变量。它的外观应如下所示:
->whereHas('dealer', function($subQuery) use($request)
{
$subQuery->where('province', $request->input($value));
})->with('dealer');
答案 1 :(得分:0)
尝试在->with('dealer')
->whereHas
$query = $query->with('dealer')
->whereHas('dealer', function($subQuery, $request, $value) {
$subQuery->where('city', $request->input($value))
->where('province', $request->input('province'));
});
答案 2 :(得分:0)
你的
$query = $query->whereHas('dealer', function($subQuery, $request, $value) {
$subQuery->where('province', $request->input($value));
})->with('dealer');
break;
应该是这样的
$search = $request->input($search);
$query = $query->whereHas('dealer', function($subQuery) use ($search) {
{
$subQuery->where('province', $search);
})->with('dealer');
你必须use ($search)
才能在回调中访问你想要的变量,因为它是另一个函数
答案 3 :(得分:0)
$query = $query->with('dealer')->whereHas('dealer', function($subQuery) use ($request, $value)
{
$subQuery->where('city', $request->input($value))
->where('province', $request->input('province'));
});
只在闭包中使用$ subQuery并使用use
注入其他变量