masterhunter
masterhunter

Reputation: 559

get id from url and passing into form in laravel 5

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

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

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

Upvotes: 3

Views: 13782

Answers (4)

Mahbub
Mahbub

Reputation: 4942

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

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

Upvotes: 2

DevK
DevK

Reputation: 9942

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']) !!}

Upvotes: 0

Balraj Allam
Balraj Allam

Reputation: 611

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

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

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

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:

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;

Upvotes: 2

Related Questions