Laravel 5 getting ID from URL

前端 未结 7 1793
一整个雨季
一整个雨季 2020-12-31 08:22

I have a table view in which I can click an button icon and redirect to another page carrying the id of the row that has been clicked.

@foreach ($pa         


        
7条回答
  •  再見小時候
    2020-12-31 08:47

    Please refer to the answered question for how to get a parameter from a route. But if you stumbled on this old Laravel thread looking for how to retrieve a model by its ID, the simplest way is to instantiate the model in the controller's request parameter:

    Route::get('/retrieve_product/{product}', 'SearchController@getProduct');
    

    Then over in your controller, you simply need:

    use App\Product;
    
    class SearchController extends Controller
    {
        public function getProduct( Product $product ) {
            return $product;
        }
    }
    

    That's it. No find, no where, no get, no first etc.

    So, in this example, if you visit /retrieve_product/1 the first product is returned to you.

提交回复
热议问题