How do I receive the key of an array in the controller in a variable?

Asked

Viewed 164 times

0

I’m sending a parameter in a view href="{{route('products.index', 'E')}}" I need to receive this parameter 'E' in a variable in the controller and send via compact(); in another view

follow my route:

Route::resource('products', 'ProductController');

follows my controller:

public function index(Request $request)
    {         
        $btn = $request->all();
        dd($btn);
//        $products = Product::latest()->paginate(5);
//        
//        return view('products.index', compact('products','btn'))
//                ->with('i', (request()->input('page', 1) -1)* 5);
    }

nesse dd($btn); a variável me retorna null como mostra abaixo.

array:1 [▼
  "E" => null
]

in this case would like to receive in the variable $btn the information 'E', someone knows how could be doing this?

Note: I am using controller Resource.

  • To get what I wanted to do, I just put the parameter {{route('products.index', 'id=E')}} I recovered the parameter in the controller

1 answer

0

Here you will have to do one by one, type in the console to see the routes that Resource is creating:
php artisan route:list

You will have to remove this route and create new ones.

Route::resource('products', 'ProductController');



example of route to take this parameter:
Route:get('products/{nomedoparametro}', 'ProductController@pegarParam')->name('products.getparam');

in the view you do so from where to send (a form for example):
{{route('products.getparam', ['nomedoparametro' => 'informacao que deseja enviar'])}}


in the controller you can do so:

public function pegarParam($nomedoparametro){
  return view('view',compact('nomedoparametro'));
}

Reference : https://laravel.com/docs/5.8/routing#route-Parameters

Browser other questions tagged

You are not signed in. Login or sign up in order to post.