Home > Back-end >  How to pass a variable right through the routes
How to pass a variable right through the routes

Time:01-03

I get an error when I try passing the var in the routes like this

<a  href="{{route('canvas',['size'=>1000])}}">
...
</a>

I pass the view like this

    public function canvas($size){
        return view('main.canvas')->with($size);
    }

this is the route I use in web.php:

Route::get('canvas',[CustomAuthController::class,'canvas'])->name('canvas');

the error I get is this: Too few arguments to ...\CustomAuthController::canvas(), 0 passed in ...\Controller.php on line 54 and exactly 1 expected

It seems the argument isn't being read for some reason,I tried just route('canvas',1000) but still not working

CodePudding user response:

you should use with() with accessor.

Syntax: ->with('variable-name', $data)


Your route should be.(You should allow property in URL)

Route::get('canvas/{size}', 'CustomAuthController@canvas');

And in controller

public function canvas($size){
    return view('main.canvas')->with('size',$size);
}

In blade you can

{{ $size }}
  • Related