- Chỉnh sửa lại file admin.blade.php như sau:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
@yield('title')
<!-- Google Font: Source Sans Pro -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&
display=fallback">
<!-- Font Awesome Icons -->
<link rel="stylesheet" href="{{ asset('adminlte/plugins/fontawesome-free/css/all.min.css') }}">
<!-- Theme style -->
<link rel="stylesheet" href="{{ asset('adminlte/dist/css/adminlte.min.css') }}">
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
@include('partials.header')
@include('partials.siderbar')
@yield('content')
@include('partials.footer')
</div>
<!-- jQuery -->
<script src="{{ asset('adminlte/plugins/jquery/jquery.min.js') }}"></script>
<!-- Bootstrap 4 -->
<script src="{{ asset('adminlte/plugins/bootstrap/js/bootstrap.bundle.min.js') }}"></script>
<!-- AdminLTE App -->
<script src="{{ asset('adminlte/dist/js/adminlte.min.js') }}"></script>
</body>
</html>
- Vào Navicat kết nối Mysql đặt tên localhost. Tạo mới database đặt tên như sau:
- Vào lại Php Storm của dự án tìm đếm thư mục vendor chọn .env và sửa như sau:DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=shopping
DB_USERNAME=root
DB_PASSWORD=
- Chạy lại serve lên và kiểm tra.
- Vào lại Php storm tạo mới một của sổ nhập :
php artisan make:model Category -m
- Vào kiểm tra :
- Vào kiểm tra :<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->integer('parent_id')->default(0);
$table->string('slug');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
- Vào App => Providers => app serviceProvider.php nhập mã nguồn sau ;
- Vào terminal nhập php artisan migrate.
- Kiểm tra trong database ta có :
- Trong navicat ta có :- Tạo dữ liệu cho bảng :
<div class="col-md-12">
<a href="" 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">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</div>
- Text lại trên trình duyệt :
- Tạo mới sử dụng teminal:
php artisan make:controller CategoryController
Route::prefix('categories')->group(function () {
Route::get('/create',[
'as'=> 'categories.create',
'uses' => 'CategoryController@create'
]);
});
- Sử dụng bôi đen CategoryController nhấn ctrl+shift+n để tìm đến file.
class CategoryController extends Controller
{
public function create(){
return view('category.add');
}
}
<!-- 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">
- Vào Resources => Views => Tạo mới thư mục category trong thư mục tạo file add.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) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0">Starter Page</h1>
</div><!-- /.col -->
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item active">Starter Page</li>
</ol>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->
<!-- Main content -->
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<form>
<div class="form-group">
<label>Tên danh mục</label>
<input type="email" class="form-control"
placeholder="Nhập tên danh mục">
</div>
<div class="form-group">
<label>Chọn danh mục</label>
<select class="form-control" >
<option value="0">Chọn danh mục cha</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
@endsection
- Kiểm tra trình duyệt:
- Vào CategoryController sửa lại như sau :
class CategoryController extends Controller
{
public function create(){
return view('category.add');
}
public function index(){
return view('category.index');
}
}
- Vào Resources => Views => Tạo mới thư mục category trong thư mục tạo file 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) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0">Starter Page</h1>
</div><!-- /.col -->
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item active">Starter Page</li>
</ol>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.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">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
@endsection
- Vào siderbar.blade.php sửa lại : {{ route('categories.index') }}
<li class="nav-item">
<a href="{{ route('categories.index') }}" class="nav-link">
<i class="nav-icon fas fa-th"></i>
<p>
Danh mục sản phẩm
<span class="right badge badge-danger">New</span>
</p>
</a>
</li>
- sửa lại đường dẫn:
<!-- Brand Logo -->
<a href="index3.html" class="brand-link">
<img src="{{asset('adminlte/dist/img/AdminLTELogo.png')}}" alt="AdminLTE Logo" class="brand-image img-circle elevation-3" style="opacity: .8">
<span class="brand-text font-weight-light">AdminLTE 3</span>
</a>
<!-- Sidebar -->
<div class="sidebar">
<!-- Sidebar user panel (optional) -->
<div class="user-panel mt-3 pb-3 mb-3 d-flex">
<div class="image">
<img src="{{ asset('adminlte/dist/img/user2-160x160.jpg') }}" class="img-circle elevation-2" alt="User Image">
</div>
- Sửa lại đường đẫn file header:
<!-- Message Start -->
<div class="media">
<img src="{{asset('adminlte/dist/img/user1-128x128.jpg')}}" alt="User Avatar" class="img-size-50 mr-3 img-circle">
<div class="media-body">
<h3 class="dropdown-item-title">
Brad Diesel
<span class="float-right text-sm text-danger"><i class="fas fa-star"></i></span>
</h3>
<p class="text-sm">Call me whenever you can...</p>
<p class="text-sm text-muted"><i class="far fa-clock mr-1"></i> 4 Hours Ago</p>
</div>
</div>
<!-- Message End -->
</a>
<div class="dropdown-divider"></div>
<a href="#" class="dropdown-item">
<!-- Message Start -->
<div class="media">
<img src="{{asset('adminlte/dist/img/user8-128x128.jpg')}}" alt="User Avatar" class="img-size-50 img-circle mr-3">
<div class="media-body">
<h3 class="dropdown-item-title">
John Pierce
<span class="float-right text-sm text-muted"><i class="fas fa-star"></i></span>
</h3>
<p class="text-sm">I got your message bro</p>
<p class="text-sm text-muted"><i class="far fa-clock mr-1"></i> 4 Hours Ago</p>
</div>
</div>
<!-- Message End -->
</a>
<div class="dropdown-divider"></div>
<a href="#" class="dropdown-item">
<!-- Message Start -->
<div class="media">
<img src="{{asset('adminlte/dist/img/user3-128x128.jpg')}}" alt="User Avatar" class="img-size-50 img-circle mr-3">
<div class="media-body">
<h3 class="dropdown-item-title">
Nora Silvester
- Vào Views=> partials tạo mới file content-header.blade.php
<!-- Content Header (Page header) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0">{{ $name.'' . $key }}</h1>
</div><!-- /.col -->
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="#">{{$name}}</a></li>
<li class="breadcrumb-item active">{{$key}}</li>
</ol>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->
- Vào index.blade.php sửa lại :
<!-- 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">
- Vào home.blade.php sửa lại :
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
@include('partials.content-header',['name'=>'Home', 'key'=>'home']);
<!-- /.content-header -->
<!-- Main content -->
<div class="content">
<div class="container-fluid">
- Vào add.blade.php sửa lại :
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
@include('partials.content-header',['name'=>'category', 'key'=>'Add']);
<!-- /.content-header -->
<!-- Main content -->
<div class="content">
<div class="container-fluid">
- Vào navicat tạo dữ liệu cho bảng categories:
- Vào CategoryController.php sửa lại mã sau:<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
private $htmlSlelect;
public function __construct()
{
$this->htmlSlelect = '';
}
public function create(){
$htmlOption = $this->categoryRecusive(0);
return view('category.add', compact('htmlOption'));
}
function categoryRecusive($id, $text = '')
{
$data = Category::all();
foreach ($data as $value){
if ($value['parent_id'] == $id) {
$this->htmlSlelect .= "<option>" . $text . $value['name'] . "</option>";
$this->categoryRecusive($value['id'], $text . '-');
}
}
return $this->htmlSlecect;
}
public function index(){
return view('category.index');
}
}
- Vào add.blade.php sửa lại mã sau:
<div class="form-group">
<label>Chọn danh mục</label>
<select class="form-control" >
<option value="0">Chọn danh mục cha</option>
{!! $htmlOption !!}}
</select>
</div>
- Kết quả :
- Vào App => Tạo thư mục Components => Tạo file Recusive.php
<?php
namespace App\Components;
class Recusive {
private $data;
private $htmlSlelect = '';
public function __construct($data)
{
$this->data = $data;
}
public function categoryRecusive($id = 0, $text = '')
{
foreach ($this->data as $value){
if ($value['parent_id'] == $id) {
$this->htmlSlelect .= "<option value='" . $value['id'] . "'>" . $text . $value['name'] . "</option>";
$this->categoryRecusive($value['id'], $text . '--');
}
}
return $this->htmlSlelect;
}
}
- Vào add.blade.php sửa lại:
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<form action="{{ route('categories.store')}}" method="post">
@csrf
<div class="form-group">
<label>Tên danh mục</label>
<input class="form-control"
name="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>
- Vào CategoryController.php sửa như sau:
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Components\Recusive;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class CategoryController extends Controller
{
private $category;
public function __construct(Category $category)
{
$this->category = $category;
}
public function create(){
$data = $this->category->all();
$recusive = new Recusive($data);
$htmlOption = $recusive->categoryRecusive();
return view('category.add', compact('htmlOption'));
}
public function index(){
return view('category.index');
}
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');
}
}
- Vào Category.php thêm mã nguồn sau:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name', 'parent_id', 'slug'];
}
- Vào web.php thêm mã nguồn sau:
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'
]);
});
- Ra trình duyệt text:
- Vào navicat xem kết quả:


0 Nhận xét