Daniel Mer
Daniel Mer

Reputation: 39

Laravel retrieve id from URL

I would like to retrieve an id from a Laravel URL. I've tried this:

$request->route('id');

But it would tell me that $request is undefined.

The url I am using is:

http://192.168.1.14/public/tasks/create?id=1

I've got a sidebar and within that I've got an if statement.

@if((request()->is('projects/1')) || (request()->is('tasks/create')))
    <a href="{{ route('tasks.create') }}?id={{ $project->id }}">
@endif

I've hid the unnecessary code, but the second parameter is used for highlighting what page I am on in the sidebar. And the first for showing the create task option when I am on a project page.

Now, the if statement causes that its hidden ( so I can't see the error ( the error would say that there is no id but its true because it only receives the id by being on a project page with an id ))

Now, the task form on the page itself has a hidden input field with project_id so I can bind the task to the project.

Inside the value I have the $request->route('id') but it gives me an error that request is undefined.

Now I ask, how do I retrieve the id from the url, or else, can I retrieve the id another way? All the project and task blade files extend the project.index and the project.index extends the layouts.blade so its a layout within a layout.

Also, can I change the (request()->is('projects/1')) to something else so whatever project page I am on, the statement is true? For example 'projects/{project}'

Thanks for any help

Upvotes: 1

Views: 3894

Answers (2)

Rutvik Panchal
Rutvik Panchal

Reputation: 409

Create Route for getting id from url Example:

Route::name('subscriptions.store')->post('/subscriptions/store/{id}', 'CreateSubscriptionController');

Upvotes: 0

Akram Chauhan
Akram Chauhan

Reputation: 1166

Best way to do this is You don't need to write 'create'

{{ app('request')->input('id') }}

or

{{ request()->get('id') }}

IF you are not sure how it works just store in a variable and print it on view to cross-verify like this.

<?php 
$id = app('request')->input('id');
//or
$id = request()->get('id');
?>

You can always check all request URL parameters like this.

$request = request();
print_r($request);

Upvotes: 3

Related Questions