php 如何在laravel的路由名称中传递变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45752427/
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-26 03:08:22  来源:igfitidea点击:

how to pass variable in route name in laravel?

phplaravel

提问by salin kunwar

I have defined route

我已经定义了路线

Route::get('/edit-industry/{id}', 'Industries@edit')->name('admin.editIndustry');

And passing variable by

并通过变量传递

{{ route('admin.editIndustry', ['id'=>1]) }}

OR

或者

{{ route('admin.editIndustry', [1]) }}

This is not working. How to pass variable here?

这是行不通的。如何在这里传递变量?

回答by Inuyaki

wow, why are wrong answers (or answers for questions which were not asked in this case) upvoted?

哇,为什么错误的答案(或在这种情况下未提出的问题的答案)被赞成?

EkinOf is correct, you can do

EkinOf 是正确的,你可以做

{{ route('admin.editIndustry', 1) }}

Btw your first one works too and is necessary, if you have more than 1 parameter

顺便说一句,如果您有多个参数,您的第一个也可以工作并且是必要的

{{ route('admin.editIndustry', ['id'=>1]) }}
{{ route('admin.editIndustry', ['id'=>1, 'something'=>42]) }}

回答by EkinOf

If you have only one parameter you can do that :

如果您只有一个参数,您可以这样做:

{{ route('admin.editIndustry', 1) }}

回答by M.J

Passing single parameter:

传递单个参数:

##Defining Route:##
Route::get('edit-industry/{id}', ['as' => 'admin.editIndustry', 'uses' => 'Industries@edit']);

##Calling Route:##
{{ route('admin.editIndustry',[$id]) }}

Passing multiple parameter:

传递多个参数:

##Defining Route:##
Route::get('edit-industry/{id}/{step}', ['as' => 'admin.editIndustry', 'uses' => 'Industries@edit']);

##Calling Route:##
{{ route('admin.editIndustry',[$id, $step]) }}

回答by Sagar Gautam

Simply try like this

简单地尝试这样

View

看法

{{URL::to('/edit-industry/1')}}

Route

路线

Route::get('/edit-industry/{id}', 'Industries@edit')

Controller

控制器

public function edit($id){
    // use $id here
}

Hope you understand.

希望你能理解。

回答by AddWeb Solution Pvt Ltd

Using generating URLs from Named Routesroute():

使用从命名路由生成 URLroute()

{{ route('admin.editIndustry', 1) }}

Using URLsurl():

使用URLurl():

{{url('/edit-industry', [1])}}

回答by B. Desai

You can Directly pass with route name

您可以直接通过路线名称

{{ URL::to('/edit-industry/1') }}

回答by Muaath Alhaddad

Try this, it works with me.

试试这个,它对我有用。

In your view:

在您看来:

{{ route('admin.editIndustry', $id=1) }}

In your route:

在您的路线中:

Route::post('admin/editIndustry/{id}', 'adminController@edit')-name('admin.editIndustry');