VenomRush
VenomRush

Reputation: 1682

Eloquent relationship trouble retrieving "belongsTo" relationship

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)

Upvotes: 0

Views: 193

Answers (4)

Beginner
Beginner

Reputation: 4153

your

$query = $query->whereHas('dealer', function($subQuery, $request, $value) {
    $subQuery->where('province', $request->input($value));
})->with('dealer');
break;

should be like this

$search = $request->input($search);
$query = $query->whereHas('dealer', function($subQuery) use ($search) {
{
    $subQuery->where('province', $search);
})->with('dealer');

you must use ($search) in order to access the variables you wanted in your callback since it is another function

Upvotes: 0

Amit Gupta
Amit Gupta

Reputation: 17688

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

Upvotes: 1

Laurent Meganck
Laurent Meganck

Reputation: 191

$query = $query->with('dealer')->whereHas('dealer', function($subQuery) use ($request, $value)
{
    $subQuery->where('city', $request->input($value))
        ->where('province', $request->input('province'));
});

Only use they $subQuery in your closure and use use to inject additional variables

Upvotes: 0

jonan.pineda
jonan.pineda

Reputation: 1304

Try using ->with('dealer') before ->whereHas

$query = $query->with('dealer')
    ->whereHas('dealer', function($subQuery, $request, $value) {
        $subQuery->where('city', $request->input($value))
             ->where('province', $request->input('province'));
    });

Upvotes: 0

Related Questions