Reputation: 385
How to Pass $val->id
in Laravel URL Parameter. I use Following Code. Its Return Error
href="{{url('/dashboard/medicinepending/{{$val->id}}')}}"
Upvotes: 14
Views: 104326
Reputation: 163968
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) }}
Upvotes: 31
Reputation: 3429
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] }}
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
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:
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
Upvotes: 6
Reputation: 95
By the way,i also agree with @Alexey Mezenin, you should prefer route() helper function instead of using url(), route() will give you more flexibility to pass number of params along-with the main URL.
Anyways, you shouldn't use blade code syntax ({{}}) again inside another couple of brackets and one more thing, try to avoid starting and ending slashed in the first part of url like below.
You should update your code with the following one, this will hopefully work.
href="{{ url('dashboard/medicinepending'.$val->id )}}" OR
href="{{ url('dashboard/medicinepending',$val->id )}}"
Upvotes: 2
Reputation: 1316
Try this:
href="{{ URL('/dashboard/medicinepending/'.$val->id )}}"
Upvotes: 6
Reputation: 1901
This working for me:
<a href="{{asset('/dashboard/medicinepending/'.$val->id)}}"></a>
Upvotes: 1