Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass Id in Laravel URL Parameter

Tags:

php

laravel

How to Pass $val->id in Laravel URL Parameter. I use Following Code. Its Return Error

href="{{url('/dashboard/medicinepending/{{$val->id}}')}}"
like image 226
Gokulanathan Avatar asked Mar 18 '16 04:03

Gokulanathan


People also ask

How can I get Laravel URL ID?

$request->id; You can get id from POST method like this. public function getDetail(Requests $rq){ $product = Product::where('id',$rq->id)->get(); } .

How do you pass an ID on a route?

Route::post('job/{job_id}/upload/file', 'DataController@read_xsl'); Or you can send the {job_id} as form data to the route and then do a Request to get the job_id. i.e. If your parameter name has changed from 'id' to anything, you should change both of 'id' name in route and controller to work correctly.

How do I move ID from one page to another in Laravel?

You can send the variable as route parameter. To do this change your <a> tags like this. Now pass this $course_id to your form view. To submit this variable with your other fields you can add this as hidden input field.


2 Answers

Try this:

href="{{ URL('/dashboard/medicinepending/'.$val->id )}}"
like image 102
vipul sorathiya Avatar answered Oct 01 '22 12:10

vipul sorathiya


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

like image 32
Ash Avatar answered Oct 01 '22 14:10

Ash