Quản trị dữ liệu table với Laravel hướng dẫn thực hiện xem danh sách, thêm, xóa sửa dữ liệu đang có trong table (loại sản phẩm) với Laravel
Tạo layout admin
Tạo layout adminsp;Tạo view layout
- Tạo file views/layoutadmin.blade.php
- Code
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.container >header { height: 70px; line-height: 70px}
.container > nav { height: 60px; }
</style>
<div class="container">
<header class="rows bg-info">
<h3>TECHCHAIN ADMIN WEBSITE</h3>
</header>
<nav class="row bg-secondary">
</nav>
<main class="row">
@yield('noidung','Nội dung chính')
</div>
</div>
2. Tạo menu
– Vào https://getbootstrap.com/docs/5.3/components/navbar/ và nhắp Copy

– Paste vào tag nav trong file layout admin và sửa code để được như sau

Chuẩn bị quản trị dữ liệu table
1. T>Chuẩn bị quản trị dữ liệu tablelass="wp-block-preformatted">php artisan make:controller LoaispController --resource
File LoaispController.php sẽ được tạo và có sẵn các hàm như bên dưới

2. Tạo routing cho chức năng quản trị Loaisp
Mở route>2. Tạo routing cho chức năng quản trị Loaisped">use App\Http\Controllers\LoaispController;
Route::resource('admin/loaisp',LoaispController::class);
Chúng ta sẽ tương tác với database qua Eloquent ORM. Cho >3. Tạo model ORM Loaispel thì thực hiện tạo với lệnh php artisan make:model Loaisp Và khai báo các thông số trong model như sau: Code trong hàm index() của LoaispContr>Da>1. Nạp view trong controllerez-toc-section-end">lock-preformatted">public function index() {
return view("admin/loaisp");
}
Test: http://localhost:8000/admin/loaisp – Trong LoaispController (phía trên class) , chỉ định useBootsrap cho paginator. – Trong hàm index của LoaispController: lấy 1 trang dữ liệu. Tạo file views/admin/loaisp_them.blade.php Thực hiện trong hàm create() của controller Loaisp Test>2. Nạp view trong controlleralhost:8000/admin/loaisp/create">http://localhost:8000/admin/loaisp/create Viết trong hàm store của controller Loaisp Test Chạy lệnh : php artisan make:request RuleNhapLoaisp Mở file app/Http/Requests/RuleNhapLoaisp.php và thực hiện code – Hiện các lỗi trong view: Mở views/admin/loaisp_them.blade.php, code trong tag form Code sử dụng RuleNhapLoaisp trong controller: Sử dụng trong hàm stored() Hiện các giá trị cũ user đã nhập trong form: Sử dụng hàm old() trong value của các input Test Tạo file views/admin/loaisp_edit.blade.php Thực hiện trong hàm edit () của controller Loaisp3. Tạo model ORM Loaisp
<?php //app/Models/LoaiSP.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class LoaiSP extends Model {
use HasFactory;
protected $table ="loaisp";
public $primaryKey = "id";
public $timestamps = true;
protected $fillable = ['id', 'tenLoai','soSP'];
protected $attributes= ['soSP'=>0];
}
Danh sách loại sản phẩm
1. Nạp view trong controller
2. Tạo file views/admin/loaisp.blade.php
@extends('layoutadmin')
@section('noidung')
<h1 class="text-primary p-2 h3" >QUẢN TRỊ >2. Tạo file views/admin/loaisp.blade.phpi sản phẩm -->
@endsection

3. Lấy danh sách loại từ database
3. Lấy danh sách loại từ databasenator;
Paginator::useBootstrap();
public function index() {
$perPage = 3;
$loaisp = \App\Models\Loaisp::paginate($perPage);
return view("admin.loaisp", ['loaisp'=>$loaisp] );
}
4. Hiển thị dữ liệu
<!-- views/loaisp.blade.php -->
<table class='table table-hover table-strip table-bordered'>
<tr class="bg-secondary">
<th&>4. Hiển thị dữ liệu loại</th> <th>Số SP</th><th class="text-end">Hành động</th>
</tr>
@foreach ($loaisp as $row)
<tr><td class="align-middle"> {{$row->id}} </td>
<td class="align-middle"> {{$row->tenLoai}} </td>
<td class="align-middle"> {{$row->soSP}} </td>
<td class="align-middle text-end">
<form action="/admin/loaisp/{{$row->id}}" method="post">
<a href="/admin/loaisp/{{$row->id}}/edit" class="btn btn-primary">Chỉnh</a>
<button class="btn btn-danger" type="submit">Xóa</button>
@csrf @method('DELETE')
</form>
</td>
</tr>
@endforeach
<tr> <td colspan="4"> {{ $loaisp->onEachSide(5)->links()}} </td> </tr>
</table>

Thêm loại sản phẩm
1. Tạo view – form thêm loại
Th>1. Tạo view – form thêm loạition-end">nline-color has-vivid-green-cyan-color"><!-- views/admin/loaisp_them.php -->
@extends('layoutadmin')
@section('noidung')
<form class="mx-auto p-3 border border-primary" method="post" action="/admin/loaisp">
<h1 class="text-primary p-2 h3" >THÊM LOẠI SẢN PHẨM</h1>
<div class="mb-3">
<label>Tên loại</label>
<input class="form-control" name="tenLoai" >
</div>
<div class="mb-3">
<label>Số SP</label>
<input class="form-control" name="soSP" type="number">
</div>
<div class="mb-3">
<button type="submit" class="btn btn-warning py-2 px-5" >Lưu</button>
</div> @csrf
</form>
@endsection
2. Nạp view trong controller
public function create() {
return view("admin.loaisp_them");
}

3. Code lưu database
public function store(Request $request) {
$arr = $request->post();
$tenLoai = ($request->has('tenLoai'))? $arr['tenLoai']:"";
>3. Code lưu databased="3_Code_luu_database">3. Code lưu database'))? $arr['soSP']:"";
$tenLoai = trim(strip_tags($tenLoai));
$soSP = (int) $soSP;
$loai = new \App\Models\Loaisp;
$loai->tenLoai = $tenLoai;
$loai->soSP = $soSP;
$loai->save();
return redirect('/admin/loaisp');
}

4. Validate form với request class
<?php //app/Http/Requests/RuleNhapLoaisp.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class RuleNhapLoaisp extends FormRequest {
public function authorize(){ return true; }
public function rules() {return [
'tenLoai' => ['required','min:3','max:20'],
'soSP' => ['required','integer', 'min:1'],
];
}
public function messages(){ return [
'tenLoai.required' => 'Bạn chưa nhập tên loại',
'tenLoai.min' => 'Tên loại ngắn quá vậy',
'tenLoai.max' => 'Tên loại dài quá',
'soSP.required' => 'Bạn chưa nhập số sản phẩm',
'soSP.min' => 'Nhập số SP>=1 nhé'
];
}
}
@if ($errors->any())
<div class="alert alert-info p-2">
<ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul>
</div>
@endif



Sửa loại sản phẩm
1. Tạo view – form chỉnh loại
<!-- views/admin/loaisp_edit.php -->
@extends('layoutadmin')
@section('noidung')
<form class="mx-auto p-3 border border-primary" method="post" action="/admin/loaisp/{{$loaisp->id}}">
<h>Sửa>1. Tạo view – form chỉnh loạiection-end">-2 h3" >CHỈNH LOẠI SẢN PHẨM</h1>
<div class="mb-3">
<label>Tên loại</label>
<input value="{{$loaisp->tenLoai}}" class="form-control" name="tenLoai" >
</div>
<div class="mb-3">
<label>Số SP</label>
<input value="{{$loaisp->soSP}}" class="form-control" name="soSP" type="number">
</div>
<div class="mb-3">
<button type="submit" class="btn btn-warning py-2 px-5" >Lưu</button>
</div> @csrf {{ method_field('PUT') }}
</form>
@endsection
2. Nạp view trong controller
public function edit(Request $request,$id=0){
$loaisp = \App\Models\Loaisp::find($id);
if ($loaisp==null) {
$request->session()->flash('thongbao', "Loại sp $id không có");
return redirect("/thongbao");
}
return view("admin.loaisp_edit", ['loaisp'=>$loa>2. Nạp view trong controllerlock-paragraph">Test xem form http://localhost:8000/admin/loaisp/1/edit

3. Code lưu database
Viết trong hàm update() của controller Loaisp
public function update(RuleNhapLoaisp $request, $id) {
$arr = $request->post();
$tenLoai = ($request->has('tenLoai'))? $arr['tenLoai']:"";
$soSP = ($request->has('soSP'))? $arr['soSP']:"";
$tenLoai = trim(strip_tags($tenLoai));
$soSP = (int) $soSP;
$loai = \App\Models\Loaisp::find($id);
if ($loai == null) {
$request->session()->flash('thongbao', "Loại sản phẩm $id không tồn tại");
redirect("/thongbao");
}
$loai->tenLoai = $tenLoai;
$loai->soSP = $soSP;
$loai->save();
return redirect('/admin/loaisp');
}
Test



Xóa loại sản phẩm
1. Hỏi trước khi xóa
Mở file loaisp.blade.php và thêm code vào nút Xóa:
<!-- views/loaisp.blade.php -->
<button onclick="return confirm('Xóa hả?')" class="btn btn-danger" type="submit">
Xóa
</button>
Xó>1. Hỏi trước khi xóa
toc-section-end">a loại trong controllerCode trong hàm destroy của LoaispController
public function destroy($id) {
$loai = \App\Models\Loaisp::find($id);
if ($loai == null) {
$request->session()->flash('thongbao', "Loại sp $id không có");
redirect("/thongbao");
}
$loai->delete();
return redirect('/admin/loaisp');
}
Test

3. Kiểm tra trong loại có sản phẩm không
Các em tự thực hiện nhé
Bài viết Quản trị dữ liệu table với Laravel đã hướng dẫn bạn thực hiện quản trị dữ liệu trong table, cụ thể là table loại sản phẩm . Đây là phần quan trọng của công đoạn thực hiện quản trị website. Để quản trị các table khác, các bạn thực hiện tương tự như trên nhé.
>3. Kiểm tra trong loại có sản phẩm không


