从 url 获取 id 并传递到 laravel 5 中的表单

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

get id from url and passing into form in laravel 5

laravellaravel-5

提问by masterhunter

i defined a route as

我定义了一条路线

Route::get('roundtables/{name}/tags/store',['as'=>'tags.store','uses'=>'TagController@store','middleware'=>['owner','auth']]);

In my view, i have a form in this url

在我看来,我在这个网址中有一个表格

http://localhost:8000/roundtables/1/tags

http://localhost:8000/roundtables/1/tags

<div class="col-md-3">
        <div class="well">
            {!! Form::open(['route'=>'tags.store','method'=>'GET']) !!}
            <h2>New Tags</h2>
            {{ Form::label('name','Name') }}
            {{ Form::text('name',null,['class'=>'form-control']) }}

            {{Form::submit('Create New Tag',['class'=>'btn btn-primary btn-block btn-h1-spacing'])}}
        </div>

    </div>

My problem is, how to get the id from url which is id '1' and passing into the form when user clicked submit.

我的问题是,如何从 url 获取 id id '1' 并在用户单击提交时传递到表单中。

My controller

我的控制器

public function store(Request $request,$name)
{
    $this->validate($request,array('name'=>'required|max:255'));
    $tag=new Tag;
    $tag->name=$request->name;
    $tag->roundtable_id=$name;
    $tag->save();


    Session::flash('success','New Tag was successfully added');

    return redirect()->route('tags.index');
}

采纳答案by Alexey Mezenin

When you're using custom routes for CRUD, avoid using standard RESTful method and route names. Before building the form, you need to pass this variable to the view:

当您为 CRUD 使用自定义路由时,请避免使用标准的 RESTful 方法和路由名称。在构建表单之前,您需要将此变量传递给视图:

public function createTag($name)
{
    ....
    return view('form', compact('name'));
}

Define your route as:

将您的路线定义为:

Route::get('roundtables/{name}/tags/storeTag',['as'=>'tags.storeTag','uses'=>'TagController@storeTag','middleware'=>['owner','auth']]);

Then pass variable to from the form:

然后将变量传递给表单:

{!! Form::open(['route' => ['tags.storeTag', $name], 'method'=>'GET']) !!}

And get it in the controller:

并在控制器中获取它:

public function storeTag(Request $request, $name)
{
    echo $name;

回答by Mahbub

You get the wildcardvalue by using request()helper method easily.

您可以通过使用辅助方法轻松获取通配符request()

{{request()->route('name')}}

回答by Balraj Allam

In your TagController

在你的标签控制器中

public function index($name)
{
    $tags= Tag::where($name)->first();


    // add name parameter to return
    return view('tags.index')->withTags($tags)->withName($name);
}

And in your view

在你看来

<div class="col-md-3">
        <div class="well">


            //edit this form tag
            {!! Form::open(['route'=>['tags.store',$name],'method'=>'GET']) !!}

            <h2>New Tags</h2>
            {{ Form::label('name','Name') }}
            {{ Form::text('name',null,['class'=>'form-control']) }}

            {{Form::submit('Create New Tag',['class'=>'btn btn-primary btn-block btn-h1-spacing'])}}
        </div>

    </div>

And in your TagController@store

而在你 TagController@store

public function store(Request $request,$name){
         echo $name;
}

回答by DevK

This should work.

这应该有效。

Request::segment(2)

Or pass it from controller:

或者从控制器传递它:

public function index($name)
{
    $tags= Tag::where($name)->first();
    return view('tags.index')->withTags($tags)->with('name', $name);
}

Then just pass it into the form:

然后只需将其传递到表单中:

{!! Form::open(['route'=>['tags.store', $name],'method'=>'GET']) !!}