How to Pass Id in Laravel URL Parameter

Try this:

href="{{ URL('/dashboard/medicinepending/'.$val->id )}}"

Try this:

{{ url('/dashboard/medicinepending/', [$val->id]) }}

But it's a bad idea to use relative paths, because if you'll want to change URL scheme, you will need to manually find tens or hundreds links in your views and controllers and change them manually. In this case you'll definitely forget to change some of them and some pages of your app will be broken.

A good practice is to use links to route:

{{ route('name.route', ['id' => $val->id]) }}

Or links to action:

{{ action('MyController@show', $val->id) }}

Quick Answer

As previously explained the way in which to build a URL within a blade template you can use the url helper; for your example this would work:

{{ url('dashboard/medicinepending/', [$val->id] }}

Best Practice

It is not advised to use url, route or even action when building a url for your views. This is because the URLs will not change, if they do you should have 301 redirects set-up in order for your SEO score to remain valid and good.

So simply do something like:

<a href="/dashboard/medicinepending/{{ $val->id }}">

But if you REALLY want to use a wrapper then url is the second best thing.

The only purpose of these helpers are for e-mails or commands via artisan, when fired the cron does not know about the URL only what is defined in the config: app.url

Explanation

The way in which URLs are built with laravel is by prepending the string with the value of app.url config; found: inside config/app.php

This is mainly for the CLI, when you have cron processes running and generating e-mails from a queue with links, therefore this could be used within your application. Otherwise do not use it at all.

Using the url helper is good because you can change the value of the app.url config by your own Service Provider. This is very useful for when you have a website in multiple languages and/or load a sites language based on the domain name; example:

  • domain.com - would load the US website
  • domain.uk - would load the UK website
  • domain.eu - would load a generic EU website
  • domain.de - would load the German website
  • domain.jp - would load the Japanese website
  • etc

As also mentioned you can build urls from a route; this is only valid if you name your routes which is not always the case.

{{ route('name.route', ['id' => $val->id] }}

This will find the route by the name route.name and then parse that end-point through app('url') which happens to be the UrlGenerator - so this is no better than the url helper - do not go out of your way to name all routes - that is bad practice.

Another practice mentioned which is also not a good idea is using action by providing the class and action to find a route:

{{ action('MyController@show', $val->id) }}

This is a bad idea unless MyController is actually an interface NOT a physical class, in that you need Inversion of Control to inject the class that implements MyController otherwise your application will not conform to the S.O.L.I.D Principle

Tags:

Php

Laravel