samedi 1 septembre 2018

How to fix Undefined variable: request in laravel 5.6

I am going to filter data with districtname in my vehicles table like this,

public function index()
    {
       $vehicles = Vehicle::with('uploads')
                ->when($request->district, function ($query, $request) {
                    return $query->where('districtname', $request->district);
                })->get();

        return view('vehicles.index')->withVehicles($vehicles);
    }

and my vehicle table like this,

id  districtname  vehiclename 
1    alabama          toyota
2    alamaba          ford
3    miyuri           bmw
4    misisipi         benz
5    miyuri           ford
6    alabama          toyota

and routes is like this,

Route::get('district', [
    'uses' => 'VehicleController@index',
    'as'   => 'districts.index',

]);

but got following error msg,

Undefined variable: request
in VehicleController.php line 39

how can fix this???



via Chebli Mohamed

1 commentaire:

  1. import libreary request

    use Illuminate\Http\Request;

    public function index(Request $request)
    {
    $vehicles = Vehicle::with('uploads')
    ->when($request->district, function ($query, $request) {
    return $query->where('districtname', $request->district);
    })->get();

    return view('vehicles.index')->withVehicles($vehicles);
    }

    RépondreSupprimer