[英]How to pass value inside href to laravel controller?
這是我的視圖文件中的代碼片段。
@foreach($infolist as $info)
<a href="">{{$info->prisw}} / {{$info->secsw}}</a>
@endforeach
這是我在路由文件中定義的路由
Route::get('switchinfo','SwitchinfoController');
我想將 href 標記內的兩個值傳遞給上面的路由並在控制器中檢索它們。 有人可以提供代碼來做這件事嗎?
由於您試圖將兩個參數傳遞給控制器,
您的控制器可能如下所示:
<?php namespace App\Http\Controllers;
class SwitchinfoController extends Controller{
public function switchInfo($prisw, $secsw){
//do stuffs here with $prisw and $secsw
}
}
你的路由器可能看起來像這樣
$router->get('/switchinfo/{prisw}/{secsw}',[
'uses' => 'SwitchinfoController@switchInfo',
'as' => 'switch'
]);
然后在你的刀片中
@foreach($infolist as $info)
<a href="{!! route('switch', ['prisw'=>$info->prisw, 'secsw'=>$info->secsw]) !!}">Link</a>
@endforeach
為您的路線命名:
Route::get('switchinfo/{parameter}', [
'as'=> 'test',
'uses'=>'SwitchinfoController@function'
]);
傳遞一個包含你想要的參數的數組
<a href="{{route('test', ['parameter' => 1])}}">
{{$info->prisw}} / {{$info->secsw}}
</a>
並在控制器功能中使用
function ($parameter) {
// do stuff
}
或者,如果不想將參數綁定到 url,只需要$_GET
參數,如url/?parameter=1
你可以像這樣使用它
Route::get('switchinfo', [
'as'=> 'test',
'uses'=>'SwitchinfoController@function'
]);
function (){
Input::get('parameter');
}
您可以簡單地在您的網址中傳遞參數,例如
@foreach($infolist as $info)
<a href="{{ url('switchinfo/'.$info->prisw.'/'.$info->secsw.'/') }}">
{{$info->prisw}} / {{$info->secsw}}
</a>
@endforeach
和路線
Route::get('switchinfo/{prisw}/{secsw}', 'SwitchinfoController@functionname');
並在控制器中發揮作用
public functionname($prisw, $secsw){
// your code here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.