Skip to content

Instantly share code, notes, and snippets.

@kamaroly
Created January 27, 2020 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamaroly/b44eb82f514a8cf9e4960e348b25f09d to your computer and use it in GitHub Desktop.
Save kamaroly/b44eb82f514a8cf9e4960e348b25f09d to your computer and use it in GitHub Desktop.
This trait adds path to your eloquent model ( $article->edit_path, $article->show_path, $article->update_path,$article->destroy_path)
use Exception;
trait EloquentRoutePaths {
/**
* Actions that are allowed to be performed on this
* Model
* @var array
*/
protected $actions = ['show','edit','update','destroy'];
/**
* Route name of this model, default is table name
* @var string
*/
protected $baseRouteName;
/**
* Get path for contribution
* @return string
*/
protected function path($action = 'edit')
{
if (! in_array($action,$this->actions)) {
throw new Exception("Invalid Route Action in Model passed", 1);
}
// If user exciplicitly defined the route property
// use it instead of default table name
if (empty($this->baseRouteName)) {
$this->baseRouteName = $this->getTable();
}
return route($this->baseRouteName.'.'.$action,$this);
}
/**
* Get edit path for this models
* @return string
*/
public function getEditPathAttribute()
{
return $this->path('edit');
}
/**
* Get show path for the route
* @return string
*/
public function getShowPathAttribute()
{
return $this->path('show');
}
/**
* Update Path for this model
* @return string
*/
public function getUpdatePathAttribute()
{
return $this->path('update');
}
/**
* Destroy path for this model
* @return string
*/
public function getDestroyPathAttribute()
{
return $this->path('destory');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment