title: Laravel Custom Route Model Binding
body: <p>I have the following setup:</p>
  
  <p>routes.php</p>
  
  <pre><code>Route::get('{page?}', [
      'uses'=&gt;'PageController@getPage',
      'as'=&gt;'page'
  ])-&gt;where('page', '(.*)?');
  </code></pre>
  
  <p>RouteServiceProvider.php</p>
  
  <pre><code>$router-&gt;bind('page', function($value, $route)
  {
      if($value == "/"){ $value = "home"; };
      $explodedPage = explode("/",$value);
      $page = Page::findBySlug(last($explodedPage));
      if(!isset($page)){
          \App::abort(404);
      }
      $ancestors = $page-&gt;ancestorsAndSelf()-&gt;get();
      $sections=array();
      foreach($ancestors as $ancestor)
      {
          $sections[]=$ancestor-&gt;slug;
      }
      if(implode("/",$sections)==$value){
          return $page;
      }else{
          return $page;
          //Else Redirect
      }
  });
  </code></pre>
  
  <p>Page.php</p>
  
  <pre><code>use Baum\Node;
  use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  use URL;
  use Venturecraft\Revisionable\RevisionableTrait;
  use Illuminate\Database\Eloquent\SoftDeletes;
  use Cviebrock\EloquentSluggable\SluggableInterface;
  use Cviebrock\EloquentSluggable\SluggableTrait;
  
  class Page extends Node implements SluggableInterface
  {
  
      use RevisionableTrait, SoftDeletes, SluggableTrait;
  
      protected $sluggable = array(
          'build_from' =&gt; 'title',
          'save_to'    =&gt; 'slug',
      );
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = ['title', 'description', 'content', 'owner_id', 'system', 'status'];
  
      /**
       * The attributes excluded from the model's JSON form.
       *
       * @var array
       */
      protected $hidden = ['parent_id','lft','rgt','depth'];
  
      /**
       * The attributes excluded from revision
       *
       * @var array
       */
      protected $dontKeepRevisionOf = ['updater_id','parent_id','lft','rgt','depth'];
  
  }
  </code></pre>
  
  <p>URLS look like such: </p>
  
  <pre><code>localhost/ (uses pre-defined slug)  
  localhost/page-slug  
  localhost/parent-slug/page-slug  
  localhost/parent-parent-slug/parent-slug/page-slug  
  Etc...  
  </code></pre>
  
  <p>Retrieving pages works fine; but my question is in regards to generating the URL  </p>
  
  <pre><code>{{URL::route('page',$page)}}
  </code></pre>
  
  <p>Simply Generates,
      localhost/page-id</p>
  
  <p>I know I can do:</p>
  
  <pre><code>{{URL::route('page',['page'=&gt;$page-&gt;generateURLString()])}}
  </code></pre>
  
  <p>But I would much rather do this cleaner if possible. Does anyone have any recommendations?</p>