【问题标题】:get id from url and passing into form in laravel 5从 url 获取 id 并在 laravel 5 中传递给表单
【发布时间】:2017-05-21 00:17:17
【问题描述】:

我将路线定义为

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

在我看来,我在这个 url 中有一个表单

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>

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

我的控制器

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');
}

【问题讨论】:

  • 向我们展示您的控制器代码

标签: laravel laravel-5


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    这应该可行。

    Request::segment(2)
    

    或从控制器传递:

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

    然后将其传递到表单中:

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

    【讨论】:

    • 嗨,但我无法将数据传递给控制器​​,因为出现错误“缺少 [Route: tags.store] [URI: roundtables/{name}/tags/store] 所需的参数。(查看: C:\wamp64\www\blog\resources\views\tags\index.blade.php)"
    • 哦,我明白了。您没有在表单中传递参数。在 form::open: 'route'=&gt;['tags.store', $name],'method'=&gt;'GET'] 中使用它
    【解决方案3】:

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

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

    将您的路线定义为:

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

    然后将变量传递给表单:

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

    并在控制器中获取它:

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

    【讨论】:

      【解决方案4】:

      在你的 TagController 中

      public function index($name)
      {
          $tags= Tag::where($name)->first();
      
      
          // add name parameter to return
          return view('tags.index')->withTags($tags)->withName($name);
      }
      

      在你看来

      <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>
      

      在你的TagController@store

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

      【讨论】:

        猜你喜欢
        • 2016-01-20
        • 1970-01-01
        • 1970-01-01
        • 2016-11-25
        • 2016-04-01
        • 2016-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多