-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14a9ad1
commit 9e420ee
Showing
15 changed files
with
1,175 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\StoreBrandRequest; | ||
use App\Http\Requests\UpdateBrandRequest; | ||
use App\Models\Brand; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\View\View; | ||
|
||
class BrandController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
*/ | ||
public function index(Request $request): View | ||
{ | ||
$page = $request->query('page'); | ||
$search = $request->query('search'); | ||
|
||
$brands = Brand::when($search, function (Builder $query, ?string $search) { | ||
$query->where('name', 'LIKE', "%{$search}%") | ||
->orWhere('description', 'LIKE', "%{$search}%"); | ||
}) | ||
->latest() | ||
->paginate() | ||
->withQueryString(); | ||
|
||
if ($page > $brands->lastPage() && $page > 1) { | ||
abort(404); | ||
} | ||
|
||
return view('brands.index', compact('brands', 'search')); | ||
} | ||
|
||
/** | ||
* Show the form for creating a new resource. | ||
*/ | ||
public function create(): View | ||
{ | ||
return view('brands.create'); | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
*/ | ||
public function store(StoreBrandRequest $request): RedirectResponse | ||
{ | ||
$brand = new Brand; | ||
$brand->name = $request->input('name'); | ||
$brand->description = $request->input('description'); | ||
$brand->save(); | ||
|
||
return redirect()->route('brands.index') | ||
->with('message', 'The brand has been created.'); | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
*/ | ||
public function show(Brand $brand): View | ||
{ | ||
return view('brands.show', compact('brand')); | ||
} | ||
|
||
/** | ||
* Show the form for editing the specified resource. | ||
*/ | ||
public function edit(Brand $brand): View | ||
{ | ||
return view('brands.edit', compact('brand')); | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
*/ | ||
public function update(UpdateBrandRequest $request, Brand $brand): RedirectResponse | ||
{ | ||
$brand->name = $request->input('name'); | ||
$brand->description = $request->input('description'); | ||
$brand->save(); | ||
|
||
return redirect()->route('brands.index') | ||
->with('message', 'The brand has been updated.'); | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
*/ | ||
public function destroy(Brand $brand): RedirectResponse | ||
{ | ||
$brand->delete(); | ||
|
||
return redirect()->route('brands.index') | ||
->with('message', 'The brand has been deleted.'); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class StoreBrandRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'name' => [ | ||
'required', | ||
'string', | ||
'min:3', | ||
'max:100', | ||
'unique:brands,name', | ||
], | ||
'description' => [ | ||
'nullable', | ||
'string', | ||
'min:3', | ||
'max:255', | ||
], | ||
]; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\Rule; | ||
|
||
class UpdateBrandRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'name' => [ | ||
'required', | ||
'string', | ||
'min:3', | ||
'max:100', | ||
Rule::unique('brands', 'name')->ignore($this->brand), | ||
], | ||
'description' => [ | ||
'nullable', | ||
'string', | ||
'min:3', | ||
'max:255', | ||
], | ||
]; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
|
||
class Brand extends Model | ||
{ | ||
use HasFactory, HasUuids; | ||
|
||
/** | ||
* The table associated with the model. | ||
* | ||
* @var string | ||
*/ | ||
protected $table = 'brands'; | ||
|
||
/** | ||
* The primary key associated with the table. | ||
* | ||
* @var string | ||
*/ | ||
protected $primaryKey = 'id'; | ||
|
||
/** | ||
* Indicates if the model's ID is auto-incrementing. | ||
* | ||
* @var bool | ||
*/ | ||
public $incrementing = false; | ||
|
||
/** | ||
* The data type of the auto-incrementing ID. | ||
* | ||
* @var string | ||
*/ | ||
protected $keyType = 'string'; | ||
|
||
/** | ||
* Indicates if the model should be timestamped. | ||
* | ||
* @var bool | ||
*/ | ||
public $timestamps = true; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array<int, string> | ||
*/ | ||
protected $fillable = [ | ||
'name', | ||
'description', | ||
]; | ||
|
||
/** | ||
* Get the assets for the brand. | ||
*/ | ||
public function assets(): HasMany | ||
{ | ||
return $this->hasMany(Asset::class, 'brand_id', 'id'); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
database/migrations/2024_09_22_082913_create_brands_table.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('brands', function (Blueprint $table) { | ||
$table->uuid('id')->primary(); | ||
$table->string('name')->unique(); | ||
$table->text('description')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('brands'); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Database\Seeders; | ||
|
||
use App\Models\Brand; | ||
use Illuminate\Database\Seeder; | ||
|
||
class BrandSeeder extends Seeder | ||
{ | ||
/** | ||
* Run the database seeds. | ||
*/ | ||
public function run(): void | ||
{ | ||
Brand::factory()->count(5) | ||
->create(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ public function run(): void | |
{ | ||
$this->call([ | ||
CategorySeeder::class, | ||
BrandSeeder::class, | ||
]); | ||
} | ||
} |
Oops, something went wrong.