- Vào Navicat xóa dữ liệu category : Ctrl + a + delete.
Nhập ALTER TABLE categories AUTO_INCEREMENT = 1
Thêm lại dữ liệu trên PhpStorm Và xem kết quả trên navicat:
- Vào index.blade.php :<!-- Stored in resources/views/child.blade.php -->
@extends('layouts.admin')
@section('title')
<title>Trang chủ</title>
@endsection
@section('content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
@include('partials.content-header',['name'=>'category', 'key'=>'List']);
<!-- /.content-header -->
<!-- Main content -->
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<a href="{{ route('categories.create') }}" class="btn btn-success float-right mr-2">add</a>
</div>
<div class="col-md-12">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Tên danh mục sản phẩm</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach($categories as $category)
<tr>
<th scope="row">{{$category->id}}</th>
<td>{{ $category->name }}</td>
<td>
<a href="{{ route('categories.edit', ['id'=>$category->id]) }}"
class="btn btn-default">Edit</a>
<a href="{{ route('categories.delete', ['id'=>$category->id]) }}"
class="btn btn-danger">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="col-md-12">
{{ $categories->links() }}
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
@endsection
- Vào web.php:
Route::post('/store',[
'as'=> 'categories.store',
'uses' => 'CategoryController@store'
]);
Route::get('/edit/{id}',[
'as'=> 'categories.edit',
'uses' => 'CategoryController@edit'
]);
Route::get('/delete/{id}',[
'as'=> 'categories.delete',
'uses' => 'CategoryController@delete'
]);
- Vào Resources => views => category =>Tạo mới file edit.blade.php:
<!-- Stored in resources/views/child.blade.php -->
@extends('layouts.admin')
@section('title')
<title>Trang chủ</title>
@endsection
@section('content')
<div class="content-wrapper">
@include('partials.content-header',['name'=>'category', 'key'=>'Add']);
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<form action="{{ route('categories.update',['id'=>$category->id]) }}" method="post">
@csrf
<div class="form-group">
<label>Tên danh mục</label>
<input class="form-control"
name="name"
value="{{$category->name}}"
placeholder="Nhập tên danh mục">
</div>
<div class="form-group">
<label>Chọn danh mục</label>
<select class="form-control"
name="parent_id">
<option value="0">Chọn danh mục cha</option>
{!! $htmlOption !!}}
</select>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
- Vào Category.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name', 'parent_id', 'slug'];
}
- Vào index.blade.php:
<!-- Stored in resources/views/child.blade.php -->
@extends('layouts.admin')
@section('title')
<title>Trang chủ</title>
@endsection
@section('content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
@include('partials.content-header',['name'=>'category', 'key'=>'List']);
<!-- /.content-header -->
<!-- Main content -->
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<a href="{{ route('categories.create') }}" class="btn btn-success float-right mr-2">add</a>
</div>
<div class="col-md-12">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Tên danh mục sản phẩm</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach($categories as $category)
<tr>
<th scope="row">{{$category->id}}</th>
<td>{{ $category->name }}</td>
<td>
<a href="{{ route('categories.edit', ['id'=>$category->id]) }}"
class="btn btn-default">Edit</a>
<a href="{{ route('categories.delete', ['id'=>$category->id]) }}"
class="btn btn-danger">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="col-md-12">
{{ $categories->links() }}
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
@endsection
- Vào Recusive.php:
<?php
namespace App\Components;
class Recusive {
private $data;
private $htmlSlelect = '';
public function __construct($data)
{
$this->data = $data;
}
public function categoryRecusive($parentId, $id = 0, $text = '')
{
foreach ($this->data as $value){
if ($value['parent_id'] == $id) {
if (!empty($parentId)&& $parentId ==$value['id']){
$this->htmlSlelect .= "<option selected value='" . $value['id'] . "'>" . $text . $value['name'] . "</option>";
}else{
$this->htmlSlelect .= "<option value='" . $value['id'] . "'>" . $text . $value['name'] . "</option>";
}
$this->categoryRecusive($parentId, $value['id'], $text . '--');
}
}
return $this->htmlSlelect;
}
}
- Vào CategoryController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
use App\Components\Recusive;
use Illuminate\Support\Str;
class CategoryController extends Controller
{
private $category;
public function __construct(Category $category)
{
$this->category = $category;
}
public function create(){
$htmlOption = $this->getCategory($parentId='');
return view('category.add', compact('htmlOption'));
}
public function index(){
$categories = $this->category->latest()->paginate(5);
return view('category.index', compact('categories'));
}
public function store(Request $request)
{
$this->category->create([
'name'=>$request->name,
'parent_id'=>$request->parent_id,
'slug'=>Str::slug($request->name)
]);
return redirect()->route('categories.index');
}
public function getCategory($parentId)
{
$data = $this->category->all();
$recusive = new Recusive($data);
$htmlOption = $recusive->categoryRecusive($parentId);
return $htmlOption;
}
public function edit($id)
{
$category = $this->category->find($id);
$htmlOption = $this->getCategory($category->parent_id);
return view('category.edit', compact('category','htmlOption'));
}
public function update($id,Request $request)
{
$this->category->find($id)->update([
'name'=>$request->name,
'parent_id'=>$request->parent_id,
'slug'=>Str::slug($request->name)
]);
return redirect()->route('categories.index');
}
public function delete($id)
{
}
}
- Vào web.php:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', function () {
return view('home');
});
Route::prefix('categories')->group(function () {
Route::get('/',[
'as'=> 'categories.index',
'uses' => 'CategoryController@index'
]);
Route::get('/create',[
'as'=> 'categories.create',
'uses' => 'CategoryController@create'
]);
Route::post('/store',[
'as'=> 'categories.store',
'uses' => 'CategoryController@store'
]);
Route::get('/edit/{id}',[
'as'=> 'categories.edit',
'uses' => 'CategoryController@edit'
]);
Route::post('/update/{id}',[
'as'=> 'categories.update',
'uses' => 'CategoryController@update'
]);
Route::get('/delete/{id}',[
'as'=> 'categories.delete',
'uses' => 'CategoryController@delete'
]);
});
- Xong chức năng Edit.
- Vào terminal chạy lệnh sau :
php artisan make:migration add_column_deleted_at_table_categories --table=categories
- Vào bảng ...add_column_deleted_at_table_categories.php:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddColumnDeletedAtTableCategories extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('categories', function (Blueprint $table) {
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('categories', function (Blueprint $table) {
$table->dropColumn('deleted_at');
});
}
}
- Vào teminal chạy lệnh : php artisan migrate(Nếu bị lỗi vào navicat xóa dữ liệu và chạy lại lệnh)
- Vào web.php:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', function () {
return view('home');
});
Route::prefix('categories')->group(function () {
Route::get('/',[
'as'=> 'categories.index',
'uses' => 'CategoryController@index'
]);
Route::get('/create',[
'as'=> 'categories.create',
'uses' => 'CategoryController@create'
]);
Route::post('/store',[
'as'=> 'categories.store',
'uses' => 'CategoryController@store'
]);
Route::get('/edit/{id}',[
'as'=> 'categories.edit',
'uses' => 'CategoryController@edit'
]);
Route::post('/update/{id}',[
'as'=> 'categories.update',
'uses' => 'CategoryController@update'
]);
Route::get('/delete/{id}',[
'as'=> 'categories.delete',
'uses' => 'CategoryController@delete'
]);
});
- Vào CategoryController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
use App\Components\Recusive;
use Illuminate\Support\Str;
class CategoryController extends Controller
{
private $category;
public function __construct(Category $category)
{
$this->category = $category;
}
public function create(){
$htmlOption = $this->getCategory($parentId='');
return view('category.add', compact('htmlOption'));
}
public function index(){
$categories = $this->category->latest()->paginate(5);
return view('category.index', compact('categories'));
}
public function store(Request $request)
{
$this->category->create([
'name'=>$request->name,
'parent_id'=>$request->parent_id,
'slug'=>Str::slug($request->name)
]);
return redirect()->route('categories.index');
}
public function getCategory($parentId)
{
$data = $this->category->all();
$recusive = new Recusive($data);
$htmlOption = $recusive->categoryRecusive($parentId);
return $htmlOption;
}
public function edit($id)
{
$category = $this->category->find($id);
$htmlOption = $this->getCategory($category->parent_id);
return view('category.edit', compact('category','htmlOption'));
}
public function update($id,Request $request)
{
$this->category->find($id)->update([
'name'=>$request->name,
'parent_id'=>$request->parent_id,
'slug'=>Str::slug($request->name)
]);
return redirect()->route('categories.index');
}
public function delete($id)
{
$this->category->find($id)->delete();
return redirect()->route('categories.index');
}
}
- Vào Category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Category extends Model
{
use SoftDeletes;
protected $fillable = ['name', 'parent_id', 'slug'];
}
- Kết quả của cột delete_at: khi nhấn delete sản phẩm:

0 Nhận xét