Skip to content

Commit

Permalink
Inserting category and sub-category: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Slymee committed Dec 21, 2023
1 parent ee25758 commit 37b5d71
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
24 changes: 20 additions & 4 deletions app/Http/Controllers/CategoryAndSubCategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,31 @@ public function index()

//Add Category Form
public function addCategoryFormDisplay(){
return view('modals.adminAddCategory');
$datas = CategoryAndSubCategory::all();
return view('modals.adminAddCategory', ['datas' => $datas]);
}

/**
* Store a newly created resource in storage.
* Insert new category.
*/
public function store(Request $request)
public function insertCategory(Request $request)
{
//
try{
$request->validate([
'category_name' => ['bail', 'required'],
'parent_id' => ['nullable', 'exists:category_and_sub_categories,id'],
]);


CategoryAndSubCategory::create([
'category_name' => $request->category_name,
'parent_id' => $request->parent_id,
]);

return redirect()->back()->with('message', 'Insert Success.');
}catch(\Exception $e){
return redirect()->back()->with('message', $e->getMessage());
}
}

/**
Expand Down
16 changes: 16 additions & 0 deletions app/Models/CategoryAndSubCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,20 @@
class CategoryAndSubCategory extends Model
{
use HasFactory;

protected $table = 'category_and_sub_categories';

protected $fillable = [
'category_name',
'parent_id',
];


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

public function parent(){
return $this->belongsTo(CategoryAndSubCategory::class, 'parent_id');
}
}
17 changes: 16 additions & 1 deletion resources/css/admin-dashboard-category.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
.form-container{
padding: 10px;
width: 40%;
height: 30%;
height: 37%;
background: white;
position: absolute;
top: 50%;
Expand Down Expand Up @@ -92,3 +92,18 @@ input[type=text]:focus{
::placeholder{
color: rgb(61,81,181);
}



.message{
color: red;
font-size: 18px;
}

select{
padding: 10px;
width: 100%;
font-size: 18px;
margin-top: 10px;
color: rgb(61,81,181);
}
3 changes: 2 additions & 1 deletion resources/views/backend/adminCategory.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
<tr>
<th>SN</th>
<th>Category Name</th>
<th colspan="2">Utilities</th>
<th colspan="3">Utilities</th>
</tr>

<tr>
<td>1</td>
<td>Fruit</td>
<td><button>Sub-categories</button></td>
<td><button>Edit</button></td>
<td><button>Delete</button></td>
</tr>
Expand Down
22 changes: 19 additions & 3 deletions resources/views/modals/adminAddCategory.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,26 @@
<div class="content-container">
<div class="form-container">
<span>Add a Category</span>
<form action="" autocomplete="off">
<label for="category-name">Category Name </label><br>
<input type="text" name="category-name" placeholder="Enter Category Name"><br>
<form action={{ route('admin.insert.category') }} autocomplete="off" method="POST">
@csrf
<label for="category_name">Category Name </label><br>
<input type="text" name="category_name" placeholder="Enter Category Name"><br>
<select name="parent_id" id="">
<option value="" selected disabled>-- Select Sub-category --</option>
@if ($datas)
@foreach ($datas as $data)
@if(!($data->parent_id))
<option value={{ $data->id }}>{{ $data->category_name }}</option>
@endif
@endforeach
@endif
</select>
<input type="submit" name="" id="" value="Create New Category">

@if (session('message'))
<span class="message">Category Inserted</span>
@endif

</form>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
//admin dashboard
Route::get('/dashboard', [DashboardController::class, 'index'])->middleware('auth')->name('admin.dashboard');
Route::get('/admin-category', [CategoryAndSubCategoryController::class, 'index'])->middleware('auth')->name('category.and.subcategory');
Route::get('admin-category-add', [CategoryAndSubCategoryController::class, 'addCategoryFormDisplay'])->name('add.category.form');
Route::get('/admin-category-add', [CategoryAndSubCategoryController::class, 'addCategoryFormDisplay'])->name('add.category.form');
Route::post('/admin-category-add/insert', [CategoryAndSubCategoryController::class, 'insertCategory'])->name('admin.insert.category');


//Forgot Password Routess
Expand Down

0 comments on commit 37b5d71

Please sign in to comment.