0

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???

Shadow
  • 33,525
  • 10
  • 51
  • 64
banda
  • 402
  • 1
  • 7
  • 27
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Qirel Sep 01 '18 at 08:06
  • The `$request` variable is not visible inside the index method, you don't pass an argument with that name. – Qirel Sep 01 '18 at 08:07
  • then got **Missing argument 1 for App\Http\Controllers\VehicleController::index()** err msg – banda Sep 01 '18 at 08:12
  • 1
    You must be pass param in `public function index(Request $request) {}`. Be remember to add `Request` namespace [check](https://laravel.com/docs/5.6/requests). – Jaydeep Mor Sep 01 '18 at 08:20
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Shadow Sep 01 '18 at 08:33
  • did public function index(Request $request) {} but got **Trying to get property of non-object** – banda Sep 01 '18 at 09:38

2 Answers2

1

When you call when() on the query builder, the first argument is the value you want to pass to the callback, that is passed as the second argument.

When you do

->when($request->district, function ($query, $request) {
  return $query->where('districtname', $request->district);
})

then $request->district is passed to the callback, provided it has a value, therefore there is no need to try to access its district property.

To fix that, change the above to:

->when($request->district, function ($query, $district) {
  return $query->where('districtname', $district);
})
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
0

The error

Undefined variable: request
in VehicleController.php line 39

Is because of not using REQUEST in the function index

You will have to use Illuminate\Http\Request in your class as well as you will need to pass the REQUEST obj in your index function.

Check the below code to use it.

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SomeController extends Controller
{

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

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

}