8

This is the code I have so far:

Web.php

Route::get('/{uri}', 'PageController@show')->name('page.show');

PageController

// Show the requested page
public function show(Page $page)
{
    return view('templates.page', compact('page'));
}

Page model

public function getRouteKeyName()
{
    return 'uri';
}

My question is how come the Route-model-binding is not working and not finding the page in the controller even though I have changed the route key name. It just has an empty model in the controller and has not found the page.

Danny Guest
  • 93
  • 1
  • 4
  • 1
    You misunderstood the whole thing, read the [doc](https://laravel.com/docs/5.6/routing#route-model-binding). The route param name must match with the type hinted variable name. The `getRouteKeyName ` helps customizing to query in the `db` using that key other than `id`. – The Alpha May 08 '18 at 12:40
  • That makes sense I changed the line of code in web.php to: Route::get('/{page}', 'PageController@show')->name('page.show'); This now works, Thank you. Is this best practice though? – Danny Guest May 08 '18 at 12:43

1 Answers1

13

You should do something like the following:

// Route
Route::get('/{page}', 'PageController@show')->name('page.show');

// Controller Method
public function show(Page $page)
{
    return view('templates.page', compact('page'));
}

If /{page} contains an id like: 1 and your pages table has id column then all done but if you would like to query the pages table other than an id then declare the getRouteKeyName method in your Page model and retuen that column name from that method. So for example, if your pages table has unique slug and your uri has something like example.com/contact then declare the following method:

public function getRouteKeyName()
{
    return 'slug'; // db column name
}

Now, the framework will query for a page using something like where slug = {slug from uri} other than id/default. Hope it helps now.

The Alpha
  • 143,660
  • 29
  • 287
  • 307