Skip to content
This repository was archived by the owner on Oct 15, 2023. It is now read-only.

Commit 3d248e9

Browse files
fix and update
1 parent 5eae583 commit 3d248e9

File tree

17 files changed

+670
-96
lines changed

17 files changed

+670
-96
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Backend\Admin;
4+
5+
use App\Helper\ResponseHelper;
6+
use App\Models\Category;
7+
use Illuminate\Http\Request;
8+
use App\Http\Controllers\Controller;
9+
use App\Http\Requests\StoreCategory;
10+
use App\Http\Requests\UpdateCategory;
11+
use Illuminate\Support\Str;
12+
use Yajra\DataTables\DataTables;
13+
14+
class CategoriesController extends Controller
15+
{
16+
/**
17+
* Display a listing of the resource.
18+
*
19+
* @return \Illuminate\Http\Response
20+
*/
21+
public function index(Request $request)
22+
{
23+
if($request->ajax()) {
24+
$categories = Category::anyTrash($request->trash)->get();
25+
return DataTables::of($categories)
26+
->addColumn('name_my', function($category) {
27+
$name_str = json_decode($category->name);
28+
return $name_str->my;
29+
})
30+
->addColumn('name_en', function ($category) {
31+
$name_str = json_decode($category->name);
32+
return $name_str->en;
33+
})
34+
->editColumn('parent_id', function($category) {
35+
if($category->parent_id) {
36+
$encoded = json_decode($category->parent->name);
37+
return $encoded->my . ' (' . $encoded->en . ')';
38+
}
39+
return '-';
40+
})
41+
->addColumn('plus-icon', function() {
42+
return null;
43+
})
44+
->addColumn('action', function ($category) use ($request) {
45+
$detail_btn = '';
46+
$restore_btn = '';
47+
$edit_btn = '<a class="edit text text-primary mr-2" href="' . route('admin.categories.edit', ['category' => $category->id]) . '"><i class="far fa-edit fa-lg"></i></a>';
48+
49+
if ($request->trash == 1) {
50+
$restore_btn = '<a class="restore text text-warning mr-2" href="#" data-id="' . $category->id . '"><i class="fa fa-trash-restore fa-lg"></i></a>';
51+
$trash_or_delete_btn = '<a class="destroy text text-danger mr-2" href="#" data-id="' . $category->id . '"><i class="fa fa-minus-circle fa-lg"></i></a>';
52+
} else {
53+
$trash_or_delete_btn = '<a class="trash text text-danger mr-2" href="#" data-id="' . $category->id . '"><i class="fas fa-trash fa-lg"></i></a>';
54+
}
55+
56+
return "${detail_btn} ${edit_btn} ${restore_btn} ${trash_or_delete_btn}";
57+
})
58+
->rawColumns(['plus-icon', 'name_my', 'name_en', 'action'])
59+
->make(true);
60+
}
61+
return view('backend.admin.categories.index');
62+
}
63+
64+
/**
65+
* Show the form for creating a new resource.
66+
*
67+
* @return \Illuminate\Http\Response
68+
*/
69+
public function create()
70+
{
71+
$active_categories = Category::noTrash();
72+
$main_categories = $active_categories->main()->get();
73+
$suggest_rank = $active_categories->max('rank') + 1;
74+
return view('backend.admin.categories.create', compact('main_categories', 'suggest_rank'));
75+
}
76+
77+
/**
78+
* Store a newly created resource in storage.
79+
*
80+
* @param \Illuminate\Http\Request $request
81+
* @return \Illuminate\Http\Response
82+
*/
83+
public function store(StoreCategory $request)
84+
{
85+
$name_str = json_encode(['my' => $request->name_my, 'en' => $request->name_en]);
86+
$slug = Str::slug($request->slug ?? $request->name_en);
87+
$parent_id = $request->parent_id;
88+
$found = false;
89+
90+
$check_slug = Category::where('slug', $slug)->first();
91+
if($check_slug) {
92+
return back()->withErrors(['msg' => 'Link is already exist.'])->withInput();
93+
}
94+
95+
if(! empty($parent_id)) {
96+
$found = Category::find($parent_id);
97+
}
98+
99+
Category::create([
100+
'name' => $name_str,
101+
'slug' => $slug,
102+
'rank' => abs($request->rank) ?? (Category::maxRank() + 1),
103+
'parent_id' => ($found) ? $parent_id : 0
104+
]);
105+
106+
return redirect()->route('admin.categories.index')->with('success', 'New Category Successfully Created.');
107+
}
108+
109+
/**
110+
* Display the specified resource.
111+
*
112+
* @param int $id
113+
* @return \Illuminate\Http\Response
114+
*/
115+
public function show(Category $category)
116+
{
117+
return view('backend.admin.categories.show', compact('category'));
118+
}
119+
120+
/**
121+
* Show the form for editing the specified resource.
122+
*
123+
* @param int $id
124+
* @return \Illuminate\Http\Response
125+
*/
126+
public function edit(Category $category)
127+
{
128+
$main_categories = Category::noTrash()->main()->get();
129+
return view('backend.admin.categories.edit', compact('category', 'main_categories'));
130+
}
131+
132+
/**
133+
* Update the specified resource in storage.
134+
*
135+
* @param \Illuminate\Http\Request $request
136+
* @param int $id
137+
* @return \Illuminate\Http\Response
138+
*/
139+
public function update(UpdateCategory $request, Category $category)
140+
{
141+
$name_str = json_encode(['my' => $request->name_my, 'en' => $request->name_en]);
142+
$slug = Str::slug($request->slug ?? $request->name_en);
143+
$parent_id = $request->parent_id;
144+
$found = false;
145+
146+
$check_slug = Category::whereNotIn('id', [$category->id])->where('slug', $slug)->first();
147+
if ($check_slug) {
148+
return back()->withErrors(['msg' => 'Link is already exist.'])->withInput();
149+
}
150+
151+
if (!empty($parent_id)) {
152+
$found = Category::find($parent_id);
153+
}
154+
155+
$category->update([
156+
'name' => $name_str,
157+
'slug' => $slug,
158+
'rank' => abs($request->rank) ?? (Category::maxRank() + 1),
159+
'parent_id' => ($found) ? $parent_id : 0
160+
]);
161+
162+
return redirect()->route('admin.categories.index')->with('success', 'Successfully Updated.');
163+
}
164+
165+
/**
166+
* Remove the specified resource from storage.
167+
*
168+
* @param int $id
169+
* @return \Illuminate\Http\Response
170+
*/
171+
public function destroy(Category $category)
172+
{
173+
$category->delete();
174+
return ResponseHelper::success();
175+
}
176+
177+
public function trash(Category $category)
178+
{
179+
$category->trash();
180+
return ResponseHelper::success();
181+
}
182+
183+
public function restore(Category $category)
184+
{
185+
$category->restore();
186+
return ResponseHelper::success();
187+
}
188+
}

app/Http/Requests/StoreCategory.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class StoreCategory extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'name_my' => 'required',
28+
'name_en' => 'required',
29+
'link' => 'nullable|unique:catgories'
30+
];
31+
}
32+
33+
public function messages()
34+
{
35+
return [
36+
'name_my.required' => 'Name (Burmese) is required.',
37+
'name_en.required' => 'Name (English) is required.'
38+
];
39+
}
40+
}

app/Http/Requests/UpdateCategory.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class UpdateCategory extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'name_my' => 'required',
28+
'name_en' => 'required',
29+
'link' => 'nullable|unique:catgories,link,' . $this->route('category')->id
30+
];
31+
}
32+
33+
public function messages()
34+
{
35+
return [
36+
'name_my.required' => 'Name (Burmese) is required.',
37+
'name_en.required' => 'Name (English) is required.'
38+
];
39+
}
40+
}

app/Models/Category.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Traits\Trash;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Category extends Model
9+
{
10+
use Trash;
11+
12+
protected $guarded = [];
13+
14+
public function parent()
15+
{
16+
return $this->belongsTo(Category::class);
17+
}
18+
19+
public function scopeMain($query)
20+
{
21+
return $query->where('parent_id', 0);
22+
}
23+
24+
public function maxRank()
25+
{
26+
return $this->noTrash()->max('rank');
27+
}
28+
}

config/app.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
| any other location as required by the application or its packages.
1313
|
1414
*/
15-
'prefix_admin_url' => env('PREFIX_ADMIN_URL', 'backend'),
15+
'prefix_admin_url' => env('PREFIX_ADMIN_URL', '/backend'),
1616

1717
'name' => env('APP_NAME', 'Laravel'),
1818

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+
class CreateCategoriesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('categories', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->text('name')->nullable()->comment("Need to save for multiple language");
19+
$table->string('slug')->unique()->comment('for custom link');
20+
$table->bigInteger('parent_id')->default(0)->comment('for sub category');
21+
$table->integer('rank')->default(0)->comment('for sorting');
22+
$table->tinyInteger('trash')->default(0);
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('categories');
35+
}
36+
}

0 commit comments

Comments
 (0)