douwen5681 2016-10-31 07:49
浏览 31
已采纳

雄辩的关系麻烦检索“belongsTo”关系

I'm not entirely sure what the problem is because I don't understand the error message I'm receiving.

I have the following relationships on my Vehicle model

/**
 * 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');
}

If I call the method below when there's a "province" in the $request I get the following error: Missing argument 2 for App\Project\Frontend\Repo\Vehicle\EloquentVehicle::App\Project\Frontend\Repo\Vehicle{closure}()

I also get the error when there's a "city" in the request.

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);
}

I have no idea what the error means, but it points to this code line in the province switch case.

$query = $query->whereHas('dealer', function($subQuery, $request, $value)
  • 写回答

4条回答 默认 最新

  • duanniesui6391 2016-10-31 08:27
    关注

    The $request and $value variables have the info you need to use in the subquery and the way you was trying to do it was wrong. you were creating new variables instead of using existing ones. The way it should look is like the following:

    ->whereHas('dealer', function($subQuery) use($request)
        {
          $subQuery->where('province', $request->input($value));
        })->with('dealer');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料