Christian
Christian

Reputation: 61

How to pass a variable right through the routes

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

Upvotes: 2

Views: 55

Answers (1)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

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

Upvotes: 3

Related Questions