skip to Main Content

In web.php file I have route like this:

Route::get('/list/{list_id}/edit',  [ListingController::class, 'edit']);

Then in ListingController:

public function edit(Request $request) {
    var_dump($request->list_id);
    var_dump($request->filled('list_id'));
    exit;
}

If url is like: domain.com/list/20/edit

result is: string(2) "20" bool(false)

So $request->list_id exists but $request->filled('list_id') gives FALSE. What is reason for that?

(P.S. If pass parameter as usual GET parameter like ?list_id=20 then filled() method gives TRUE).

2

Answers


  1. This is because the filled() method is specifically designed to check if a value is present in the request’s input data, which includes query parameters, form data, and JSON data, but not route parameters.

    Instead you should try using the has() method like:

    $request->has('list_id')
    
    Login or Signup to reply.
  2. If you use it as a parameter in your route like domain.com/list/20/edit you need to specify it in your method:

    public function edit(Request $request, int $list_id) {
        var_dump($list_id);
        exit;
    }
    

    You can also omit the Request here as it is no longer used:

    public function edit(int $list_id) {
        var_dump($list_id);
        exit;
    }
    

    See this example in the laravel documentation on basic controllers

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search