-
-
Save khanalpride/fee06c1de3df703a32ead503a3e9dbf0 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 | |
{ | |
//example use: href="{{ $model->links['edit'] }}" | |
public function getLinksAttribute() | |
{ | |
return [ | |
'index' => route("app.{$this->routePath}.index"), | |
'show' => route("app.{$this->routePath}.show", [$this->routeKey => $this->{$this->getRouteKeyName()}]), | |
'create' => route("app.{$this->routePath}.create"), | |
'edit' => route("app.{$this->routePath}.edit", [$this->routeKey => $this->{$this->getRouteKeyName()}]), | |
'delete' => route("app.{$this->routePath}.delete", [$this->routeKey => $this->{$this->getRouteKeyName()}]), | |
]; | |
} | |
public function getRouteKeyAttribute() | |
{ | |
return strtolower(class_basename($this)); | |
} | |
public function getRoutePathAttribute() | |
{ | |
return $this->routeKey . 's'; | |
} | |
} |
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 = [ | |
'post', | |
'user', | |
'team', | |
'booking', | |
//continue with all your models. | |
]; | |
foreach ($resources as $resource) { | |
Route::livewire("/{$resource}s", "app.{$resource}s.index")->name("{$resource}s.index"); | |
Route::livewire("/{$resource}s/{{$resource}}", "app.{$resource}s.show")->name("{$resource}s.show"); | |
if($resource != 'booking') {//non-default | |
Route::livewire("/create/{$resource}", "app.{$resource}s.create")->name("{$resource}s.create"); | |
} | |
Route::livewire("/{$resource}s/edit/{{$resource}}", "app.{$resource}s.edit")->name("{$resource}s.edit"); | |
Route::livewire("/{$resource}s/delete/{{$resource}}", "app.{$resource}s.delete")->name("{$resource}s.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