style updates
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
<?php
|
||||
|
||||
return ['name' => 'Categories'];
|
||||
|
||||
+23
-3
@@ -1,8 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
return new class extends Migration {
|
||||
public function up(): void { Schema::create('categories', function (Blueprint $table): void { $table->id(); $table->foreignId('parent_id')->nullable()->constrained('categories')->nullOnDelete(); $table->string('name'); $table->string('slug')->unique(); $table->text('description')->nullable(); $table->string('type')->default('content')->index(); $table->unsignedInteger('sort_order')->default(0); $table->boolean('is_active')->default(true); $table->timestamps(); }); }
|
||||
public function down(): void { Schema::dropIfExists('categories'); }
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('categories', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('parent_id')->nullable()->constrained('categories')->nullOnDelete();
|
||||
$table->string('name');
|
||||
$table->string('slug')->unique();
|
||||
$table->text('description')->nullable();
|
||||
$table->string('type')->default('content')->index();
|
||||
$table->unsignedInteger('sort_order')->default(0);
|
||||
$table->boolean('is_active')->default(true);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('categories');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,17 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Categories\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
use Modules\Categories\Models\Category;
|
||||
|
||||
final class CategoryController extends Controller
|
||||
{
|
||||
public function index(): View { return view('categories::admin.index', ['records'=>Category::with('parent')->orderBy('type')->orderBy('sort_order')->paginate(30)]); }
|
||||
public function create(): View { return view('categories::admin.form', ['parents'=>Category::orderBy('name')->get()]); }
|
||||
public function store(Request $request): RedirectResponse { Category::create($this->validated($request)); return redirect()->route('administration.categories.index')->with('status','Category created.'); }
|
||||
public function edit(Category $category): View { return view('categories::admin.form', ['record'=>$category,'parents'=>Category::whereKeyNot($category->getKey())->orderBy('name')->get()]); }
|
||||
public function update(Request $request, Category $category): RedirectResponse { $category->update($this->validated($request)); return redirect()->route('administration.categories.index')->with('status','Category updated.'); }
|
||||
public function destroy(Category $category): RedirectResponse { $category->delete(); return back()->with('status','Category deleted.'); }
|
||||
private function validated(Request $request): array { $data=$request->validate(['parent_id'=>['nullable','exists:categories,id'],'name'=>['required','string','max:255'],'slug'=>['required','string','max:255'],'description'=>['nullable','string'],'type'=>['required','string','max:50'],'sort_order'=>['nullable','integer','min:0'],'is_active'=>['sometimes','boolean']]); $data['is_active']=$request->boolean('is_active'); return $data; }
|
||||
public function index(): View
|
||||
{
|
||||
return view('categories::admin.index', ['records' => Category::with('parent')->orderBy('type')->orderBy('sort_order')->paginate(30)]);
|
||||
}
|
||||
|
||||
public function create(): View
|
||||
{
|
||||
return view('categories::admin.form', ['parents' => Category::orderBy('name')->get()]);
|
||||
}
|
||||
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
Category::create($this->validated($request));
|
||||
|
||||
return redirect()->route('administration.categories.index')->with('status', 'Category created.');
|
||||
}
|
||||
|
||||
public function edit(Category $category): View
|
||||
{
|
||||
return view('categories::admin.form', ['record' => $category, 'parents' => Category::whereKeyNot($category->getKey())->orderBy('name')->get()]);
|
||||
}
|
||||
|
||||
public function update(Request $request, Category $category): RedirectResponse
|
||||
{
|
||||
$category->update($this->validated($request));
|
||||
|
||||
return redirect()->route('administration.categories.index')->with('status', 'Category updated.');
|
||||
}
|
||||
|
||||
public function destroy(Category $category): RedirectResponse
|
||||
{
|
||||
$category->delete();
|
||||
|
||||
return back()->with('status', 'Category deleted.');
|
||||
}
|
||||
|
||||
private function validated(Request $request): array
|
||||
{
|
||||
$data = $request->validate(['parent_id' => ['nullable', 'exists:categories,id'], 'name' => ['required', 'string', 'max:255'], 'slug' => ['required', 'string', 'max:255'], 'description' => ['nullable', 'string'], 'type' => ['required', 'string', 'max:50'], 'sort_order' => ['nullable', 'integer', 'min:0'], 'is_active' => ['sometimes', 'boolean']]);
|
||||
$data['is_active'] = $request->boolean('is_active');
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Categories\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
final class Category extends Model
|
||||
{
|
||||
protected $fillable = ['parent_id','name','slug','description','type','sort_order','is_active'];
|
||||
protected function casts(): array { return ['is_active'=>'boolean','sort_order'=>'integer']; }
|
||||
public function parent(): BelongsTo { return $this->belongsTo(self::class, 'parent_id'); }
|
||||
public function children(): HasMany { return $this->hasMany(self::class, 'parent_id')->orderBy('sort_order'); }
|
||||
protected $fillable = ['parent_id', 'name', 'slug', 'description', 'type', 'sort_order', 'is_active'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['is_active' => 'boolean', 'sort_order' => 'integer'];
|
||||
}
|
||||
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(self::class, 'parent_id');
|
||||
}
|
||||
|
||||
public function children(): HasMany
|
||||
{
|
||||
return $this->hasMany(self::class, 'parent_id')->orderBy('sort_order');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Categories\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
final class CategoriesServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
@@ -8,6 +11,7 @@ final class CategoriesServiceProvider extends ServiceProvider
|
||||
$this->app->register(RouteServiceProvider::class);
|
||||
$this->mergeConfigFrom(module_path('Categories', 'Config/config.php'), 'categories');
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$this->loadMigrationsFrom(module_path('Categories', 'Database/Migrations'));
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Categories\Providers;
|
||||
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
final class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Modules\Categories\Http\Controllers\CategoryController;
|
||||
Route::middleware(['auth','verified'])->prefix('administration/categories')->name('administration.categories.')->group(function (): void { Route::resource('/', CategoryController::class)->parameters([''=>'category'])->except('show'); });
|
||||
|
||||
Route::middleware(['auth', 'verified'])->prefix('administration/categories')->name('administration.categories.')->group(function (): void {
|
||||
Route::resource('/', CategoryController::class)->parameters(['' => 'category'])->except('show');
|
||||
});
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Categories\Tests\Feature;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
final class ModuleBootTest extends TestCase
|
||||
{
|
||||
public function test_module_configuration_is_available(): void
|
||||
|
||||
Reference in New Issue
Block a user