-
-
Save ramaID/04cdc67564643d53ae5110f521a605f0 to your computer and use it in GitHub Desktop.
No more controllers! Only Laravel LiveWire SPA routes and a handy Eloquent Model trait.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
trait HasLinks | |
{ | |
public function getLinksAttribute(): array | |
{ | |
return [ | |
'index' => $this->indexRoute, | |
'show' => $this->showRoute, | |
'create' => $this->createRoute, | |
'edit' => $this->editRoute, | |
'delete' => $this->deleteRoute, | |
]; | |
} | |
/** | |
* Build link without route model binding params | |
* Instead of href="{{ route('prefix.modelplural.action', ['modelsingular' => 'have to remember model key', 'param' => 'value']) }}" | |
* href="{{$model->actionRoute('action', ['param'=>'value'])}}" | |
* example: href="{{$model->actionRoute('subscribe', ['subscription'=>'plan_xxxx'])}}" | |
* | |
* @param string $action | |
* @param array $params | |
* @param string $route_name_prefix | |
* @return string | |
*/ | |
public function actionRoute(string $action, array $params = [], string $route_name_prefix = 'app'): string | |
{ | |
$merged = array_merge([$this->routeKey => $this->{$this->getRouteKeyName()}], $params); | |
return route("{$route_name_prefix}.{$this->routePath}.{$action}", $merged); | |
} | |
public function getIndexRouteAttribute(): string | |
{ | |
return route("app.{$this->routePath}.index"); | |
} | |
public function getShowRouteAttribute(): string | |
{ | |
return route("app.{$this->routePath}.show", [$this->routeKey => $this->{$this->getRouteKeyName()}]); | |
} | |
public function getCreateRouteAttribute(): string | |
{ | |
return route("app.{$this->routePath}.create"); | |
} | |
public function getEditRouteAttribute(): string | |
{ | |
return route("app.{$this->routePath}.edit", [$this->routeKey => $this->{$this->getRouteKeyName()}]); | |
} | |
public function getDeleteRouteAttribute(): string | |
{ | |
return route("app.{$this->routePath}.delete", [$this->routeKey => $this->{$this->getRouteKeyName()}]); | |
} | |
public function getRouteKeyAttribute(): string | |
{ | |
return strtolower(class_basename($this)); | |
} | |
public function getRoutePathAttribute(): string | |
{ | |
return \Str::plural($this->routeKey); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::group([ | |
'prefix' => 'app', | |
'as' => 'app.', | |
//'namespace' => 'App', //not needed with livewire | |
'middleware' => ['auth'], | |
], function () { | |
Route::livewire('/', 'app.dashboard')->name('dashboard'); | |
$resources = [ | |
'user', | |
'person', //becomes people | |
'event', | |
'booking', | |
//continue with all your models. | |
]; | |
foreach ($resources as $resource) { | |
$plural = \Str::plural($resource); | |
Route::livewire("/{$plural}", "app.{$plural}.index")->name("{$plural}.index"); | |
Route::livewire("/{$plural}/{{$resource}}", "app.{$plural}.show")->name("{$plural}.show"); | |
if($resource != 'booking') {//non-default | |
Route::livewire("/create/{$resource}", "app.{$plural}.create")->name("{$plural}.create"); | |
} | |
Route::livewire("/{$plural}/edit/{{$resource}}", "app.{$plural}.edit")->name("{$plural}.edit"); | |
Route::livewire("/{$plural}/delete/{{$resource}}", "app.{$plural}.delete")->name("{$plural}.delete"); | |
} | |
//non-default | |
Route::livewire('/create/booking/{event?}', "app.bookings.create")->name("bookings.create"); //event is optional | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment