how to I get value from url in controller class in laravel

Viewed 33

This is my route.

Route::get('fieldequips/{hex}', Fieldequips::class,'render')->name('fieldequips');

This is my navigation

 <x-jet-dropdown-link href="{{ route('fieldequips', ['hex'=>3]) }}" :active="request()->routeIs('fieldequips')">{{ __('Equipment') }}
</x-jet-dropdown-link>

My url looks like this http://localhost:8000/fieldequips/3 But somehow when i try to retrieve the 3 from url using my render function in Fieldequips class

public function render(Request $request)
    {


        $hex=$request()->hex;
        if($hex=3){
        $this->fieldequips = Fieldequip::all();}

        return view('livewire.fieldequips');
    }

It always fail, how do i format it to the correct way?

1 Answers

You may still type-hint the Illuminate\Http\Request and access your route parameter hex by defining your controller method like the following:

Here is the link:

<a href="{{ route('fieldequips', 3) }}">Equipment</a>

Route:

Route::get('/fieldequips/{hex}', [Fieldequips::class,'render'])->name('fieldequips');

Controller:

public function render(Request $request, $hex)
    {
        if($hex=3){
        $this->fieldequips = Fieldequip::all();}

        return view('livewire.fieldequips');
    }

https://laravel.com/docs/7.x/requests

Related