Skip to content

Commit

Permalink
feat: add brands feature
Browse files Browse the repository at this point in the history
  • Loading branch information
memiljamel committed Sep 23, 2024
1 parent 14a9ad1 commit 9e420ee
Show file tree
Hide file tree
Showing 15 changed files with 1,175 additions and 3 deletions.
99 changes: 99 additions & 0 deletions app/Http/Controllers/BrandController.php
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.');
}
}
40 changes: 40 additions & 0 deletions app/Http/Requests/StoreBrandRequest.php
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',
],
];
}
}
41 changes: 41 additions & 0 deletions app/Http/Requests/UpdateBrandRequest.php
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',
],
];
}
}
66 changes: 66 additions & 0 deletions app/Models/Brand.php
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 database/migrations/2024_09_22_082913_create_brands_table.php
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');
}
};
18 changes: 18 additions & 0 deletions database/seeders/BrandSeeder.php
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();
}
}
1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function run(): void
{
$this->call([
CategorySeeder::class,
BrandSeeder::class,
]);
}
}
Loading

0 comments on commit 9e420ee

Please sign in to comment.