php 如何在laravel 5.1中使用url(路由)传递多个参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34217120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:51:24  来源:igfitidea点击:

How to pass multiple arguments with url (routing) in laravel 5.1

phplaravellaravel-5.1

提问by Rajendra

LARAVEL 5.1

拉维尔 5.1

I want to edit my table which having ID and TktID.

我想编辑具有 ID 和 TktID 的表。

I want to pass this two parameters to edit method of my TestController.

我想将这两个参数传递给我的 TestController 的编辑方法。

My link:

我的链接:

<a href="/sig/edit?id={{$value->id}}&ticketid={{$value->ticketid}}" title="Edit signature">

My route.php

我的路线.php

Route::get('sig/edit{id}{ticketid}','TicketsController@edit');

edit method of controller:

控制器的编辑方法:

 public function edit($id, $ticketid)
    {
        //
    }

How do I pass here two arguments in route.php to controller.

我如何将 route.php 中的两个参数传递给控制器​​。

回答by Saiyan Prince

You forget end bracket

你忘了结束括号

You have error in your routes.php file:

您的 routes.php 文件中有错误:

Route::get('sig/edit{id}{ticketid}', 'TicketsController@edit');

Should be:

应该:

Route::get('sig/edit/{id}/{ticketid}', 'TicketsController@edit');

Notice the forward slash after editand id.

注意editid之后的正斜杠。

And in the view it should be either of the following:

在视图中它应该是以下之一:

<a href="{{ url('sig/edit/ ' . $value->id . '/' . $value->ticketid .')}}" title="Edit signature">

Or

或者

<a href="/sig/edit/{$value->id}/{$value->ticketid}" title="Edit signature">

I hope this helps you out. Cheers.

我希望这能够帮到你。干杯。

回答by PRANAV

 <a class="getValues" href="/sig/edit" title="Edit signature"/>Edit</a>
    <input type="hidden" id="id" name="id" value"={{$value->id}}"/>
    <input type="hidden" id="ticketid" name="ticketid" value="{{$value->ticketid}}"/>

jQuery(document).ready(function(){
     var $id=jQuery('#id').val();
     var $ticketid=jQuery('#ticketid').val();

     jQuery('getValues').on('click',function(){
          $.ajax({
            url:'yourController/controller'sFunction',
                  data:{'id':$id,'ticketid':$ticketid},
         }).success(function(response){
                alert(rseponse);
            });
     })
});

paste this line of code as first line in your controller's function ...

将此代码行粘贴为控制器函数中的第一行...

$inputs = Input::all();

and get values of input like

并获得输入值,如

echo    $ticketid=$inputs['ticketid'];
 echo "<br/>";
 echo     $id=$inputs['id'];

回答by Aris Ram

Route

路线

Route::get('sig/edit{id}{ticketid}','TicketsController@edit')->name(sig.edit);

Route::get('sig/edit{id}{ticketid}','TicketsController@edit')->name(sig.edit);

link

关联

<a href="{{route('sig.edit',[$value->id,$value->ticketid])}}" title="Edit signature">

<a href="{{route('sig.edit',[$value->id,$value->ticketid])}}" title="Edit signature">

回答by Akhil Mohandas

I found this way to keep your URL the same way and access multiple parameters

我发现这种方法可以使您的 URL 以相同的方式访问多个参数

<a href="/sig/edit?id={{$value->id}}&ticketid={{$value->ticketid}}" title="Edit signature">

Route

路线

Route::get('sig/edit', 'TicketsController@edit');

Access the parameter values in the controller

访问控制器中的参数值

Controller

控制器

public function edit(){
$id = Input::get('id');
$ticketId = Input::get('ticketid');
}

Note: import Input in controller

注意:在控制器中导入输入

use Illuminate\Support\Facades\Input;

回答by rust

in routes/web.phpfile - This one works for me.

routes/web.php文件中 - 这个对我有用。

Route::any('/documents/folder/{args?}', function($args){
   $args = explode('/', $args);
   return $args;
})->where('args', '(.*)');

It should handle every argument/parameter now.

它现在应该处理每个参数/参数。

Hope it works !

希望它有效!

回答by DEEPAK

As you are passing the parameters like this ?name=value, you dont have to set up the route for it , you can directly access it in your controller by Dependency injection

当你传递这样的参数时?name=value,你不必为它设置路由,你可以通过依赖注入直接在你的控制器中访问它

you have to add this above your class

你必须在你的班级上方添加这个

use Illuminate\Http\Request;

Then in controller inject it & fetch the parameter values by name:

然后在控制器中注入它并按名称获取参数值:

    public function edit(Request $request)
    {
        //
    $id= $request->id;
 $tkt= $request->tkt_id;
    }


回答by Sark

In my case, I am passing two parameters like this:

就我而言,我传递了两个这样的参数:

ROUTES

路线

Route::get('/add/{debitid}/{creditid}',
    ['as'=>'loan_add',
    'uses'=>'LoanController@loanset']);

In LoanController

在贷款控制器中

public function loanset($debitid, $creditid)
{
    $debit_user= UserModel::findOrFail($debitid);
    $credit_user= UserModel::findOrFail($creditid);
    return view('load.add',compact('debit_user','credit_user'));
}

This example might be helpful.

这个例子可能会有所帮助。