20 C
Jaipur
Thursday, October 29, 2020

Explaining Route Model Binding – Linux Hint

Must read

How To Use pulseaudio-dlna To Stream Audio From Ubuntu 20.10 To Chromecast Devices

pulseaudio-dlna is a streaming server which allows streaming audio from your Linux computer to a Chromecast or DLNA / UPNP device in the same...

Nokia cuts full-year profit forecast, announces new strategy – Latest News

STOCKHOLM/HELSINKI: Nokia cut its full-year profit forecast even as the telecom network equipment maker's quarterly underlying profit met expectations in its first earnings under...

payments technology: JPMorgan backs UK payments technology company Icon Solutions – Latest News

JPMorgan Chase & Co has made a strategic investment in Icon Solutions, a UK-based company that advises and provides technology to banks to help...

tcl 4k tv: TCL expands its smart TV lineup with TVP615 Android TV, price starts at Rs 23,999

Expanding its smart TV lineup, consumer electronics brand TCL has launched its latest 4K UHD TV -- TVP615 in India. The TV runs

One of the most common routing patterns is that the first line of any controller method tries to find the resource with the given ID.

Route::get(‘stores/{id}’, function($id) {
    $stores = Store::find($id);
});

Laravel provides a feature that simplifies this pattern called “route model binding.” This allows you to define that a particular parameter name (e.g., ({stores}) will indicate to the route resolver that it should look up an Eloquent record with that ID and then pass it in as the parameter instead of just passing the ID.

There are two kinds of route model binding: implicit and custom (or explicit).

Implicit Route Model Binding

The simplest way to use route model binding is to name your route parameter something unique to that model (e.g., name it $store instead of $id), then typehint that parameter in the closure/controller method and use the same variable name there. It’s easier to show than to describe, so take a look at the next example:

Explicit route model binding

Route::get(‘stores/{store}’, function(Store $store){
    return view(‘stores.show’)->with(‘store’, $store);
});

Because the route parameter ({store}) is the same as the method parameter ($store), and the method parameter is type hinted with a Conference model (Store $store), Laravel sees this as a route model binding. Every time this route is visited, the application will assume that whatever is passed into the URL in place of {store} is an ID that should be used to look up a Store and then that resulting model instance will be passed into your closure or controller method.

Custom Route Model Binding

To manually configure route model binding, add a line like the one in the following example to the boot() method in AppProvidersRouteServiceProvider.

public function boot(Route $router)
{
   // Just allows the parent’s boot() method to still run
   parent::boot($router);
   // Perform the binding
   $router->model(‘shop’, Store::class);
}

You’ve now defined that whenever a route has a parameter in its definition named {shop}, the route resolver will return an instance of the Store class with the ID of that URL parameter.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

How To Use pulseaudio-dlna To Stream Audio From Ubuntu 20.10 To Chromecast Devices

pulseaudio-dlna is a streaming server which allows streaming audio from your Linux computer to a Chromecast or DLNA / UPNP device in the same...

Nokia cuts full-year profit forecast, announces new strategy – Latest News

STOCKHOLM/HELSINKI: Nokia cut its full-year profit forecast even as the telecom network equipment maker's quarterly underlying profit met expectations in its first earnings under...

payments technology: JPMorgan backs UK payments technology company Icon Solutions – Latest News

JPMorgan Chase & Co has made a strategic investment in Icon Solutions, a UK-based company that advises and provides technology to banks to help...

tcl 4k tv: TCL expands its smart TV lineup with TVP615 Android TV, price starts at Rs 23,999

Expanding its smart TV lineup, consumer electronics brand TCL has launched its latest 4K UHD TV -- TVP615 in India. The TV runs

paytm: Parliamentary panel questions Paytm about Chinese investment, storing of data in servers abroad – Latest News

A parliamentary panel on Thursday questioned Paytm representatives about the quantum of Chinese investment in the company and told them that the servers on...