Skip to content

Commit

Permalink
Feature: add category parent
Browse files Browse the repository at this point in the history
  • Loading branch information
Soltee committed Mar 3, 2024
1 parent 43fd069 commit bef48fd
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 17 deletions.
16 changes: 11 additions & 5 deletions app/Http/Controllers/Admin/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public function index()
$categories = $query->paginate(2);
$total = $categories->total();

// dd(Category::pluck('id')->toArray());

return view('admin.categories.index', compact('categories', 'total'));
}

Expand All @@ -42,13 +40,21 @@ public function index()
public function store(Request $request)
{
$data = $request->validate([
'name' => 'required|string|min:3|unique:categories'
'name' => 'required|string|min:3|unique:categories',
'parent_id' => 'nullable|string|exists:categories,id'
]);

Category::create([

if($request->parent_id){

$parentIDArray = ['parent_id' => $data['parent_id']];

}

Category::create(array_merge([
'name' => $data['name'],
'slug' => Str::slug($data['name'])
]);
], $parentIDArray ?? []));

return back()->with('toast_success', 'Created');
}
Expand Down
12 changes: 12 additions & 0 deletions app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@ public function products()
{
return $this->hasMany(Product::class);
}

public function parent(){
return $this->belongsTo(Category::class, 'parent_id', 'id');
}

public function children(){
return $this->hasMany(Category::class, "parent_id", "id");
}

public function childrenCount(){
return $this->children->count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('parent_id')->nullable();
$table->string('name');
$table->string('slug')->unique();
$table->timestamps();
Expand Down
64 changes: 52 additions & 12 deletions resources/views/admin/categories/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,42 @@ class="text-md text-gray-800 hover:opacity-70 border-b border-transparent hover:
<div class="w-full mb-6 ">
<form action="{{ route('admin.categories.store') }}" method="POST" accept-charset="utf-8" enctype="multipart/form-data">
@csrf
<div class="flex flex-col items-center">
<div class="flex flex-wrap mb-6 w-full">
<label for="name" class="block w-full text-gray-900 text-sm font-bold mb-2">
{{ __('Name') }}:
</label>
<div class="flex flex-col">
<div class="flex items-center">
<div class="flex flex-wrap mb-6 w-full p-2">
<label for="parent" class="block w-full text-gray-900 text-sm font-bold mb-2">
{{ __('Parent') }}: Note : Cannot change after created
</label>

<input id="name" type="name" class="border border-gray-500 px-3 py-2 rounded w-full @error('name') border-red-600 @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
<select name="parent_id" class="border border-gray-500 px-3 py-3 rounded w-full @error('parent_id') border-red-600 @enderror">
<option value="">Select Parent Category</option>
@forelse($categories as $category)
<option value="{{ $category->id }}">
{{ $category->name }}
</option>
@empty
@endforelse
</select>
@error('parent_id')
<p class="text-c-red text-xs italic mt-4">
{{ $message }}
</p>
@enderror
</div>
<div class="flex flex-wrap mb-6 w-full p-2">
<label for="name" class="block w-full text-gray-900 text-sm font-bold mb-2">
{{ __('Name') }}:
</label>

@error('name')
<p class="text-c-red text-xs italic mt-4">
{{ $message }}
</p>
@enderror
</div>
<input id="name" type="name" class="border border-gray-500 px-3 py-2 rounded w-full @error('name') border-red-600 @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>

@error('name')
<p class="text-c-red text-xs italic mt-4">
{{ $message }}
</p>
@enderror
</div>
</div>
<button type="submit" class="px-3 py-3 w-full rounded-lg bg-gray-900 hover:bg-gray-700 text-white">
Create
</button>
Expand All @@ -62,6 +84,7 @@ class="text-md text-gray-800 hover:opacity-70 border-b border-transparent hover:
<thead>
<tr>
<th class="px-4 py-2 text-left text-capitalize text-gray-600">Name</th>
<th class="px-4 py-2 text-left text-capitalize text-gray-600">About</th>
<th class="px-4 py-2 text-left text-capitalize text-gray-600">Created At</th>
<th class="px-4 py-2 text-left text-capitalize text-gray-600"></th>
</tr>
Expand Down Expand Up @@ -90,6 +113,23 @@ class="px-3 py-3 bg-gray-900 hover:bg-gray-700 text-white rounded-r">
</form>
</div>
</td>

<td class="border px-4 py-4">

<div class="">
@if(!$category->parent_id)
<div class="">
<label class="text-md text-gray-800">Children - </label>
<span class="text-bold">{{ $category->childrenCount() }}</span>
</div>
@else
<div class="">
<label class="text-md text-gray-800">Parent - </label>
<span class="text-bold">{{ $category->parent->name }}</span>
</div>
@endif
</div>
</td>
<td class="border px-4 py-4">{{ $category->created_at->diffForHumans() }}</td>

<td class="border px-4 py-4">
Expand Down

0 comments on commit bef48fd

Please sign in to comment.