vaxilicaihouxian
vaxilicaihouxian

Reputation: 23

laravel5 generate routes key

i use laravel 5.2 now. i have these codes in my routes.php file:

Route::(['dashboard'=>'DashboardArticelController',]);

and laravel generates some router for my app :

GET /dashboard/my-articles App\Http\Controllers\DashboardArticelController@getMyArticles

here is a method in my controller:

public function getMyArticles()
{
    //$articels = Auth::user()->articals()->latest('published_at')->get(); 
    //dd(Auth::user()->articals()->latest('published_at')->simplePaginate(3));
    $articels = Auth::user()->articals()->latest('published_at')->Paginate(5);      
    return view('dashboard.view.dashboardArticelEdit',compact('articels'));
}

i wonder how the laravel5 generates this route ,i can not found the method can generates the route with the method name.

Upvotes: 2

Views: 353

Answers (1)

Lalji Nakum
Lalji Nakum

Reputation: 380

By default, Laravel assumes an Eloquent model should map to URL segments using its id column. But what if you expect it to always map to a slug?

Eloquent implements the Illuminate\Contracts\Routing\UrlRoutable contract, which means every Eloquent object has a getRouteKeyName() method on it that defines which column should be used to look it up from a URL. By default this is set to id, but you can override that on any Eloquent model:

class Test extends Model 
{
   public function getRouteKeyName()
   {
      return 'slug';
   }
}

Upvotes: 0

Related Questions