Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
No more controllers! Only Laravel LiveWire routes and a handy Eloquent Model trait.
<?php
//Laravel v7, Livewire v1
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");
Route::livewire("/{$plural}/edit/{{$resource}}", "app.{$plural}.edit")->name("{$plural}.edit");
Route::livewire("/{$plural}/delete/{{$resource}}", "app.{$plural}.delete")->name("{$plural}.delete");
if($resource != 'booking') {//non-default
Route::livewire("/create/{$resource}", "app.{$plural}.create")->name("{$plural}.create");
}
}
//non-default
Route::livewire('/create/booking/{event?}', "app.bookings.create")->name("bookings.create"); //event is optional
});
<?php
//Laravel v8, Livewire v2
//requires namespace and removed Route::livewire
Route::group([
'prefix' => 'app',
'as' => 'app.',
'namespace' => 'App\Http\Livewire\App',
'middleware' => ['auth'],
], function () {
Route::get('/', Dashboard::class)->name('dashboard');
$resources = [
'user',
'person', //becomes people
'event',
'booking',
//continue with all your models.
];
foreach ($resources as $resource) {
$plural = \Str::plural($resource);
$ucfirst = ucfirst($plural);
Route::get("/{$plural}", "{$ucfirst}\Index")->name("{$plural}.index")->middleware('onboarding');
Route::get("/{$plural}/{{$resource}}", "{$ucfirst}\Show")->name("{$plural}.show");
Route::get("/{$plural}/edit/{{$resource}}", "{$ucfirst}\Edit")->name("{$plural}.edit");
Route::get("/{$plural}/delete/{{$resource}}", "{$ucfirst}\Delete")->name("{$plural}.delete");
if($resource != 'booking') {//non-default
Route::get("/create/{$resource}", "{$ucfirst}\Create")->name("{$plural}.create");
}
}
//non-default
Route::get('/create/booking/{event?}', \Bookings\Create::class)->name("bookings.create"); //event is optional
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment