added category controller

pull/44/head
A1Gard 6 months ago
parent 16c68954ce
commit 342f1f91c4

@ -0,0 +1,122 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\XController;
use App\Http\Requests\CategorySaveRequest;
use App\Models\Access;
use App\Models\Category;
use Illuminate\Http\Request;
use App\Helper;
use function App\Helpers\hasCreateRoute;
class CategoryController extends XController
{
// protected $_MODEL_ = Category::class;
// protected $SAVE_REQUEST = CategorySaveRequest::class;
protected $cols = ['name','subtitle','parent_id'];
protected $extra_cols = ['id','slug','image'];
protected $searchable = ['name','subtitle','description'];
protected $listView = 'admin.categories.category-list';
protected $formView = 'admin.categories.category-form';
protected $buttons = [
'edit' =>
['title' => "Edit", 'class' => 'btn-outline-primary', 'icon' => 'ri-edit-2-line'],
'show' =>
['title' => "Detail", 'class' => 'btn-outline-light', 'icon' => 'ri-eye-line'],
'destroy' =>
['title' => "Remove", 'class' => 'btn-outline-danger delete-confirm', 'icon' => 'ri-close-line'],
];
public function __construct()
{
parent::__construct(Category::class, CategorySaveRequest::class);
}
/**
* @param $category Category
* @param $request CategorySaveRequest
* @return Category
*/
public function save($category, $request)
{
$category->name = $request->input('name');
$category->subtitle = $request->input('subtitle');
$category->description = $request->input('description');
$category->parent_id = $request->input('parent_id');
$category->slug = $this->getSlug($category);
if ($request->has('image')){
$category->image = $this->storeFile('image',$category, 'categories');
}
if ($request->has('bg')){
$category->bg = $this->storeFile('bg',$category, 'categories');
}
$category->save();
return $category;
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
$cats = Category::all();
return view($this->formView,compact('cats'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Category $item)
{
//
$cats = Category::all();
return view($this->formView, compact('item','cats'));
}
public function bulk(Request $request)
{
// dd($request->all());
$data = explode('.', $request->input('action'));
$action = $data[0];
$ids = $request->input('id');
switch ($action) {
case 'delete':
$msg = __(':COUNT items deleted successfully', ['COUNT' => count($ids)]);
$this->_MODEL_::destroy($ids);
break;
default:
$msg = __('Unknown bulk action : :ACTION', ["ACTION" => $action]);
}
return $this->do_bulk($msg, $action, $ids);
}
public function destroy(Category $item)
{
return parent::delete($item);
}
public function update(Request $request, Category $item)
{
return $this->bringUp($request, $item);
}
}

@ -0,0 +1,33 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CategorySaveRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return auth()->check();
}
/**
* 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:2', 'max:128'],
'subtitle' => ['nullable', 'string',],
'image' => ['nullable', 'file', 'mimes:jpg,svg,png'],
'bg' => ['nullable', 'file', 'mimes:jpg,svg,png'],
'description' => ['nullable', 'string',],
'parent_id' => ['nullable', 'exists:categories,id'],
];
}
}

@ -4,10 +4,14 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Translatable\HasTranslations;
class Category extends Model class Category extends Model
{ {
use HasFactory; use HasFactory, HasTranslations,SoftDeletes;
public $translatable = ['name', 'subtitle', 'description'];
public function imgUrl() public function imgUrl()
{ {
@ -15,14 +19,31 @@ class Category extends Model
return asset('/assets/upload/logo.svg'); return asset('/assets/upload/logo.svg');
} }
return \Storage::url('category/' . $this->image); return \Storage::url('categories/' . $this->image);
} }
public function bgUrl() public function bgUrl()
{ {
if ($this->bg == null) { if ($this->bg == null) {
return asset('/assets/upload/logo.svg'); return asset('/assets/upload/logo.svg');
} }
return \Storage::url('category/' . $this->bg); return \Storage::url('categories/' . $this->bg);
}
public function parent()
{
return $this->belongsTo(Category::class, 'parent_id');
}
public function children()
{
return $this->hasMany(Category::class, 'parent_id');
}
public function getRouteKeyName()
{
return 'slug';
} }
} }

@ -15,9 +15,11 @@ return new class extends Migration
$table->id(); $table->id();
$table->text('name'); $table->text('name');
$table->string('slug',128)->unique(); $table->string('slug',128)->unique();
$table->text('subtitle')->nullable();
$table->text('description')->nullable(); $table->text('description')->nullable();
$table->integer('sort')->default(0); $table->integer('sort')->default(0);
$table->string('image')->nullable()->default(null); $table->string('image',2048)->nullable()->default(null);
$table->string('bg',2048)->nullable()->default(null);
$table->unsignedInteger('parent_id')->nullable()->default(null)->index(); $table->unsignedInteger('parent_id')->nullable()->default(null)->index();
$table->softDeletes(); $table->softDeletes();
$table->timestamps(); $table->timestamps();

@ -0,0 +1,142 @@
@extends('admin.templates.panel-form-template')
@section('title')
@if(isset($item))
{{__("Edit category")}} [{{$item->name}}]
@else
{{__("Add new category")}}
@endif -
@endsection
@section('form')
<div class="row">
<div class="col-lg-3">
@include('components.err')
<div class="item-list mb-3">
<h3 class="p-3">
<i class="ri-message-3-line"></i>
{{__("Tips")}}
</h3>
<ul>
<li>
{{__("You can leave the slug empty; it will be generated automatically.")}}
</li>
</ul>
</div>
@if(isset($item))
<div class="item-list mb-3">
<h3 class="p-3">
<i class="ri-image-2-line"></i>
{{__('Feature image')}}
</h3>
<img src="{{$item->imgUrl()}}" alt="{{$item->name}}" class="img-fluid mb-4">
</div>
<div class="item-list mb-3">
<h3 class="p-3">
<i class="ri-image-2-line"></i>
{{__('Background image')}}
</h3>
<img src="{{$item->bgUrl()}}" alt="{{$item->name}}" class="img-fluid mb-4">
</div>
@endif
</div>
<div class="col-lg-9 ps-xl-1 ps-xxl-1">
<div class="general-form ">
<h1>
@if(isset($item))
{{__("Edit category")}} [{{$item->name}}]
@else
{{__("Add new category")}}
@endif
</h1>
<div class="row">
<div class="form-group row">
<div class="col-md-6 mt-3">
<label for="name">
{{__('Category name')}}
</label>
<input name="name" type="text" class="form-control @error('name') is-invalid @enderror"
placeholder="{{__('Category name')}}" value="{{old('name',$item->name??null)}}"/>
</div>
<div class="col-md-6 mt-3">
<label for="name">
{{__('Category slug')}}
</label>
<input name="slug" type="text" class="form-control @error('slug') is-invalid @enderror"
placeholder="{{__('Category slug')}}" value="{{old('slug',$item->slug??null)}}"/>
</div>
<div class="col-md-12 mt-3">
<div class="form-group">
<label for="subtitle">
{{__('Subtitle')}}
</label>
<input name="subtitle" type="text"
class="form-control @error('subtitle') is-invalid @enderror" id="subtitle"
placeholder="{{__('Subtitle')}}"
value="{{old('subtitle',$item->subtitle??null)}}"/>
</div>
</div>
<div class="col-md-4 mt-3">
<label for="parent">
{{__('Group Parent')}}
</label>
<select name="parent_id" id="parent"
class="form-control @error('parent') is-invalid @enderror">
<option value=""> {{__('No parent')}} </option>
@foreach($cats as $cat )
@if( !isset($item) || $cat->id != $item->id )
<option value="{{ $cat->id }}"
@if (old('parent',$item->parent_id??null) == $cat->id ) selected @endif >
{{$cat->name}}
</option>
@endif
@endforeach
</select>
</div>
<div class="col-md-4 mt-3">
<div class="form-group">
<label for="image">
{{__('Feature image')}}
</label>
<input accept=".jpg,.png,.svg" name="image" type="file"
class="form-control @error('image') is-invalid @enderror" id="image"
placeholder="{{__('Feature image')}}"/>
</div>
</div>
<div class="col-md-4 mt-3">
<div class="form-group">
<label for="bg">
{{__('Background image')}}
</label>
<input accept=".jpg,.png,.svg" name="bg" type="file"
class="form-control @error('bg') is-invalid @enderror" id="bg"
placeholder="{{__('Background image')}}"/>
</div>
</div>
<div class="col-md-12 mt-3">
<label for="description">
{{__('Description')}}
</label>
<textarea rows="5" name="description"
class="form-control @error('description') is-invalid @enderror"
placeholder="{{__('Description')}}">{{old('description',$item->description??null)}}</textarea>
</div>
<div class="col-md-12">
<label> &nbsp;</label>
<input name="" type="submit" class="btn btn-primary mt-2" value="{{__('Save')}}"/>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection

@ -0,0 +1,15 @@
@extends('admin.templates.panel-list-template')
@section('list-title')
<i class="ri-user-3-line"></i>
{{__("Categories list")}}
@endsection
@section('title')
{{__("Categories list")}} -
@endsection
@section('filter')
{{-- Other filters --}}
@endsection
@section('bulk')
{{-- <option value="-"> - </option> --}}
@endsection

@ -55,17 +55,19 @@
@endif @endif
</h1> </h1>
<div class="row">
<div class="form-group row"> <div class="form-group row">
<div class="col-md-6 mt-3"> <div class="col-md-6 mt-3">
<label for="name"> <label for="name">
{{__('Category name')}} {{__('Group name')}}
</label> </label>
<input name="name" type="text" class="form-control @error('name') is-invalid @enderror" <input name="name" type="text" class="form-control @error('name') is-invalid @enderror"
placeholder="{{__('Group name')}}" value="{{old('name',$item->name??null)}}"/> placeholder="{{__('Group name')}}" value="{{old('name',$item->name??null)}}"/>
</div> </div>
<div class="col-md-6 mt-3"> <div class="col-md-6 mt-3">
<label for="name"> <label for="name">
{{__('Category slug')}} {{__('Group slug')}}
</label> </label>
<input name="slug" type="text" class="form-control @error('slug') is-invalid @enderror" <input name="slug" type="text" class="form-control @error('slug') is-invalid @enderror"
placeholder="{{__('Group slug')}}" value="{{old('slug',$item->slug??null)}}"/> placeholder="{{__('Group slug')}}" value="{{old('slug',$item->slug??null)}}"/>
@ -77,7 +79,8 @@
</label> </label>
<input name="subtitle" type="text" <input name="subtitle" type="text"
class="form-control @error('subtitle') is-invalid @enderror" id="subtitle" class="form-control @error('subtitle') is-invalid @enderror" id="subtitle"
placeholder="{{__('Subtitle')}}" value="{{old('subtitle',$item->subtitle??null)}}"/> placeholder="{{__('Subtitle')}}"
value="{{old('subtitle',$item->subtitle??null)}}"/>
</div> </div>
</div> </div>
<div class="col-md-4 mt-3"> <div class="col-md-4 mt-3">
@ -132,6 +135,8 @@
</div> </div>
</div> </div>
</div> </div>
</div>
</div> </div>
</div> </div>
@endsection @endsection

@ -46,7 +46,7 @@
</a> </a>
</li> </li>
<li> <li>
<a> <a href="{{route('admin.category.index')}}">
<i class="ri-box-3-fill"></i> <i class="ri-box-3-fill"></i>
{{__('Product categories')}} {{__('Product categories')}}
</a> </a>

@ -52,6 +52,19 @@ Route::prefix(config('app.panel.prefix'))->name('admin.')->group(
Route::post('bulk', [\App\Http\Controllers\Admin\GroupController::class, "bulk"])->name('bulk'); Route::post('bulk', [\App\Http\Controllers\Admin\GroupController::class, "bulk"])->name('bulk');
Route::get('trashed', [\App\Http\Controllers\Admin\GroupController::class, "trashed"])->name('trashed'); Route::get('trashed', [\App\Http\Controllers\Admin\GroupController::class, "trashed"])->name('trashed');
}); });
Route::prefix('categories')->name('category.')->group(
function () {
Route::get('', [\App\Http\Controllers\Admin\CategoryController::class, 'index'])->name('index');
Route::get('create', [\App\Http\Controllers\Admin\CategoryController::class, 'create'])->name('create');
Route::post('store', [\App\Http\Controllers\Admin\CategoryController::class, 'store'])->name('store');
Route::get('edit/{item}', [\App\Http\Controllers\Admin\CategoryController::class, 'edit'])->name('edit');
Route::post('update/{item}', [\App\Http\Controllers\Admin\CategoryController::class, 'update'])->name('update');
Route::get('delete/{item}', [\App\Http\Controllers\Admin\CategoryController::class, 'destroy'])->name('destroy');
Route::get('restore/{item}', [\App\Http\Controllers\Admin\CategoryController::class, 'restore'])->name('restore');
Route::post('bulk', [\App\Http\Controllers\Admin\CategoryController::class, "bulk"])->name('bulk');
Route::get('trashed', [\App\Http\Controllers\Admin\CategoryController::class, "trashed"])->name('trashed');
});
Route::prefix('posts')->name('post.')->group( Route::prefix('posts')->name('post.')->group(
function () { function () {
Route::get('', [\App\Http\Controllers\Admin\PostController::class, 'index'])->name('index'); Route::get('', [\App\Http\Controllers\Admin\PostController::class, 'index'])->name('index');

Loading…
Cancel
Save