Skip to content

Commit 61678ed

Browse files
Update
1 parent 7e4afbb commit 61678ed

File tree

10 files changed

+356
-25
lines changed

10 files changed

+356
-25
lines changed

app/Http/Controllers/Admin/CategoryController.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Illuminate\Contracts\Support\Renderable;
66
use Illuminate\Http\Request;
77
use Illuminate\Routing\Controller;
8+
use Modules\Category\app\Http\Requests\StoreCategoryRequest;
89
use Modules\Category\app\Models\Category;
10+
use Plank\Mediable\Facades\MediaUploader;
911

1012
class CategoryController extends Controller
1113
{
@@ -32,20 +34,39 @@ public function create()
3234
* @param Request $request
3335
* @return Renderable
3436
*/
35-
public function store(Request $request)
37+
public function store(StoreCategoryRequest $request)
3638
{
37-
//
39+
$category = Category::where(['name' => $request->category['name']])->first();
40+
if ($category) {
41+
session()->flash('status', 'Category already exists.');
42+
return redirect(route('admin.category.index'))->withInput();
43+
}
44+
45+
$category = Category::create($request->category);
46+
47+
// Media
48+
if ($request->filled('image')) {
49+
$media = MediaUploader::fromSource($request->file('images'))
50+
->useHashForFilename()
51+
->onDuplicateUpdate()
52+
->upload();
53+
54+
$category->syncMedia($media, 'image');
55+
}
56+
57+
session()->flash('status', "Category ({$category->name}) created successfully.");
58+
return redirect(route('admin.category.index'));
3859
}
3960

4061
/**
4162
* Show the specified resource.
4263
* @param int Category $category
4364
* @return Renderable
4465
*/
45-
// public function show(Category $category)
46-
// {
47-
// return view('category::show');
48-
// }
66+
public function show(Category $category)
67+
{
68+
return view('category::show');
69+
}
4970

5071
/**
5172
* Show the form for editing the specified resource.
@@ -77,7 +98,7 @@ public function destroy(Category $category)
7798
{
7899
$category->delete();
79100

80-
session()->flash('status', 'Record deleted successfully.');
101+
session()->flash('status', "Category ({$category->name}) deleted successfully.");
81102
return redirect(route('admin.category.index'));
82103
}
83104
}

app/Http/Livewire/Admin/Index.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,37 @@
33
namespace Modules\Category\app\Http\Livewire\Admin;
44

55
use Livewire\Component;
6+
use Livewire\WithPagination;
7+
use Modules\Category\app\Models\Category;
68

79
class Index extends Component
810
{
11+
use WithPagination;
12+
13+
public $selected = [];
14+
15+
public $search = "";
16+
public $limit = 25;
17+
public $page = 1;
18+
19+
protected $queryString = [
20+
'search' => ['except' => ''],
21+
'page' => ['except' => 1],
22+
];
23+
924
public function render()
1025
{
11-
return view('category::livewire.admin.index');
26+
$data = [];
27+
if ($this->search) {
28+
$data['categories'] = Category::where('name', 'like', '%' . $this->search . '%')
29+
->orWhere('slug', 'like', '%' . $this->search . '%')
30+
->orWhere('description', 'like', '%' . $this->search . '%')
31+
->orderBy('name')
32+
->paginate($this->limit);
33+
} else {
34+
$data['categories'] = Category::orderBy('name')->paginate($this->limit);
35+
}
36+
37+
return view('category::livewire.admin.index', $data);
1238
}
1339
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Modules\Category\app\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class StoreCategoryRequest extends FormRequest
8+
{
9+
/**
10+
* Get the validation rules that apply to the request.
11+
*
12+
* @return array
13+
*/
14+
public function rules()
15+
{
16+
return [
17+
'category.parent_id' => ['nullable', 'uuid'],
18+
'category.name' => ['required', 'string', 'min:3', 'max:32'],
19+
'category.description' => ['nullable', 'string', 'max:2048'],
20+
'category.icon' => ['nullable', 'string', 'starts_with:fa'],
21+
'image' => ['nullable', 'image', 'mimes:png,jpg,svg']
22+
];
23+
}
24+
25+
/**
26+
* Determine if the user is authorized to make this request.
27+
*
28+
* @return bool
29+
*/
30+
public function authorize()
31+
{
32+
// return true;
33+
return $this->user()->can('category.create');
34+
}
35+
}

app/Models/Category.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
namespace Modules\Category\app\Models;
44

55
use Cviebrock\EloquentSluggable\Sluggable;
6+
use Illuminate\Database\Eloquent\Casts\Attribute;
67
use Illuminate\Database\Eloquent\Concerns\HasUuids;
78
use Illuminate\Database\Eloquent\Factories\HasFactory;
89
use Illuminate\Database\Eloquent\Model;
10+
use Plank\Mediable\Mediable;
911

1012
class Category extends Model
1113
{
12-
use HasFactory, HasUuids, Sluggable;
14+
use HasFactory, HasUuids, Mediable, Sluggable;
1315

1416
/**
1517
* The attributes that are mass assignable.
@@ -33,4 +35,24 @@ public function sluggable(): array
3335
]
3436
];
3537
}
38+
39+
/**
40+
* Get the route key for the model.
41+
*
42+
* @return string
43+
*/
44+
public function getRouteKeyName()
45+
{
46+
return 'slug';
47+
}
48+
49+
public function parent()
50+
{
51+
return $this->belongsTo(Category::class, 'parent_id');
52+
}
53+
54+
public function child()
55+
{
56+
return $this->hasMany(Category::class, 'parent_id');
57+
}
3658
}

database/Migrations/2023_03_05_100642_create_categories_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function up(): void
1616
$table->foreignUuid('parent_id')->nullable();
1717
$table->string('name');
1818
$table->string('slug')->nullable();
19+
$table->string('icon')->nullable()->default('fa fa-sitemap');
1920
$table->text('description')->nullable();
2021
$table->timestamps();
2122

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('categories', function (Blueprint $table) {
17+
if (!Schema::hasColumn('categories', 'icon')) {
18+
$table->string('icon')->after('slug')->nullable()->default('fa fa-sitemap');
19+
}
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::table('categories', function (Blueprint $table) {
31+
if (Schema::hasColumn('categories', 'icon')) {
32+
$table->dropColumn('icon');
33+
}
34+
});
35+
}
36+
};

resources/views/admin/create.blade.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
<section class="layout-top-spacing mb-4">
99
<div class="card">
10-
<h2 class="card-header mb-0 border-bottom ">{{ $title }}</h2>
11-
1210
<form method="POST" action="{{ route('admin.property.store') }}" enctype="multipart/form-data">
1311
@csrf
1412

resources/views/admin/index.blade.php

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
</x-slot> --}}
77

88
<div class="my-3">
9+
<x-alert />
10+
911
<div class="card">
1012
@can('room.create')
1113
<div class="card-header">
@@ -24,9 +26,8 @@
2426
<ul class="navbar-nav me-auto my-2 my-lg-0 navbar-nav-scroll"
2527
style="--bs-scroll-height: 100px;">
2628
<li class="nav-item">
27-
<a class="nav-link{{ request()->routeIs('admin.property.create') ? ' active' : '' }}"
28-
@if (request()->routeIs('admin.property.create')) aria-current="page" @endif
29-
href="{{ route('admin.property.create') }}">Create</a>
29+
<button class="btn btn-sm" data-bs-toggle="modal"
30+
data-bs-target="#createModal">Create</button>
3031
</li>
3132
</ul>
3233
</div>
@@ -38,4 +39,73 @@
3839
<livewire:category::admin.index />
3940
</div>
4041
</div>
42+
43+
@push('modals')
44+
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModalForm"
45+
aria-hidden="true">
46+
<div class="modal-dialog modal-dialog-centered" role="document">
47+
<div class="modal-content">
48+
<div class="modal-header">
49+
<h5 class="modal-title add-title" id="createModalFormLabel1">
50+
Add Contact
51+
</h5>
52+
{{-- <h5 class="modal-title edit-title" id="createModalFormLabel2" style="display: none;">
53+
Edit Contact</h5> --}}
54+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
55+
<i class="fa fa-times"></i>
56+
</button>
57+
</div>
58+
59+
<form id="createModalForm" method="POST" action="{{ route('admin.category.store') }}">
60+
<div class="modal-body">
61+
<div class="row gy-3">
62+
<div class="col-md-12">
63+
<div>
64+
<input type="text" id="name" class="form-control" name="category[name]"
65+
placeholder="Name" required>
66+
</div>
67+
</div>
68+
69+
<div class="col-md-12">
70+
<div class="">
71+
<textarea id="description" class="form-control" name="category[description]" placeholder="Description"></textarea>
72+
<span class="validation-text"></span>
73+
</div>
74+
</div>
75+
76+
<div class="col-md-6">
77+
<div class="input-group" x-data="{ icon: 'fa fa-sitemap' }">
78+
<span class="input-group-text">
79+
<i x-bind:class="icon"></i>
80+
</span>
81+
<input type="text" id="icon" class="form-control" name="category[icon]"
82+
x-model="icon" placeholder="Icon (fa fa-home)" value="fa fa-sitemap">
83+
</div>
84+
<span class="validation-text"></span>
85+
</div>
86+
87+
<div class="col-md-6">
88+
<div class="">
89+
<input type="file" id="image" name="image" class="form-control">
90+
</div>
91+
</div>
92+
</div>
93+
</div>
94+
<div class="modal-footer">
95+
{{-- <button id="btn-edit" class="float-left btn btn-success">Save</button> --}}
96+
97+
<button id="btn-add" class="btn btn-primary">Add</button>
98+
99+
<button class="btn bnt-sm" data-bs-dismiss="modal">
100+
<i class="fa fa-undo"></i>
101+
Cancel
102+
</button>
103+
</div>
104+
105+
@csrf
106+
</form>
107+
</div>
108+
</div>
109+
</div>
110+
@endpush
41111
</x-app-layout>

resources/views/index.blade.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)