Laravel - how to send some value of a parameter from a blade view files to a controller?

时间:2018-03-25 21:03:56

标签: javascript php ajax laravel blade

How to send value "{{$ item-> id}}" to some method in a controller?

e.g. show.blade.php - contains the value:

"{{$ item-> id}}"

MyController.php - contains the method:

public function results(Request $request){
    //and here I want to use the value {{$ item-> id}} 
}

2 个答案:

答案 0 :(得分:2)

Just make a form and post it , or have some hyperlink

<form  method="POST" action="/route_to_your_controller">
            {{ csrf_field() }} 
            <input type="hidden" name="item_id" value="{{$item->id}}" required>
</form>

Route

Route::post('/route_to_your_controller', 'yourController@results');

Controller

public function results(Request $request){
{
    $name = $request->input('item_id')     
}

答案 1 :(得分:0)

在你的MyController中使用

public function results(Request $request, Item $item){
    //and here I want to use the value {{$ item-> id}}
 $item_id = $item->id;
}

您还可以访问项目的所有字段。