samuel toh
samuel toh

Reputation: 7086

How can I get id from url with Request $request? (Laravel 5.3)

My link href on the view is like this :

<a href="{{ route('message.inbox.detail.id', ['id' => $message->id]) }}" class="btn btn-default">
    <span class="fa fa-eye"></span> {{ trans('button.view') }}
</a>

My routes is like this :

Route::get('message/inbox/detail/id/{id}', ['as'=>'message.inbox.detail.id','uses'=>'MessageController@detail']);

When clicked, the url display like this :

http://myshop.dev/message/inbox/detail/id/58cadfba607a1d2ac4000254

I want get id with Request $request

On the controller, I try like this :

public function detail(Request $request)
{
    dd($request->input('id'));
}

The result : null

How can I solve it?

Upvotes: 8

Views: 35270

Answers (4)

Dale Ryan
Dale Ryan

Reputation: 843

For people who are looking for answers using newer versions of laravel, you can get the route id using the following:

Example route with model binding

Route::put('/post/{post}, [PostController::class, 'update]);

$this->route('id') will not work in this case due to model binding.

instead you can do this:

// using in controller
request()->route('post.id);

or

// using in form request
$this->route('post.id');

Upvotes: 0

Martynas A.
Martynas A.

Reputation: 336

You can get it like this

$request->route('id')

Inside request class you can access it this way

$this->route('id')

I usually use it when I'm validating field to make sure it's unique, and I need to exclude model by ID when I'm doing update

Upvotes: 14

Irvin Chan
Irvin Chan

Reputation: 2687

This is because you're passing the id parameter through url, you should send it using a form like this

{!! Form::open(['route'=>' route('message.inbox.detail.id', 'method'=> 'POST']) !!}
 <input type="hidden" name="id" value="{{ $message->id }}" />
{!! Form::submit({{ trans('button.view') }}, ['class'=>'btn btn-default']) !!}
{!! Form::close() !!}

and now you can access via request in your controller

public function detail(Request $request)
{


 dd($request->input('id'));
}

Upvotes: 2

SteD
SteD

Reputation: 14025

Get the id via this:

public function detail($id)
{
   dd($id);
}

Upvotes: -1

Related Questions