Bài 05 : Tạo bảng sản phẩm

Bài 05 : Tạo bảng sản phẩm

 

 -    Vào AdminController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AdminController extends Controller
{
public function loginAdmin()
{
if (auth()->check()){
return redirect()->to('home');
}
return view('login');
}
public function postloginAdmin(Request $request)
{
$remember = $request->has('remember_me')? true:false;
if (auth()->attempt([
'email' => $request->email,
'password' => $request->password
], $remember)){
return redirect()->to('home');
}
}
}
-    Vào .env :
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=86400
-    Vào teminal chạy lệnh:
-    php artisan make:model Product -m        để tạo bảng Product 
-    Vào 2021_07_13_073524_create_products_table.php
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('price');
$table->string('feature_image_path')->nullable();
$table->text('content');
$table->integer('user_id');
$table->integer('category_id');
$table->timestamps();
})
;
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
-    Vào teminal chạy lệnh:
-    php artisan make:model ProductImage -m        để tạo bảng ProductImage 
-    Vào 2021_07_13_074024_create_product_images_table.php
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductImagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('image_path')->nullable();
$table->integer('product_id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_images');
}
}
-    Vào teminal chạy lệnh:
-    php artisan make:model Tag -m        để tạo bảng Tag 
-    Vào 2021_07_13_074340_create_tags_table.php
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
})
;
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tags');
}
}
-    Vào teminal chạy lệnh:
-    php artisan make:model ProductTag -m        để tạo bảng ProductTag 
-    Vào 2021_07_13_074556_create_product_tags_table.php
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_tags', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('product_id');
$table->integer('tag_id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_tags');
}
}
    Vào teminal chạy lệnh: php artisan migrate
-     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('/admin', 'AdminController@loginAdmin');
Route::post('/admin', 'AdminController@postloginAdmin');

Route::get('/home', function () {
return view('home');
});

Route::prefix('admin')->group(function () {
//category
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'
]);
});
//menu
Route::prefix('menus')->group(function () {
Route::get('/',[
'as'=> 'menus.index',
'uses' => 'MenuController@index'
]);
Route::get('/create',[
'as'=> 'menus.create',
'uses' => 'MenuController@create'
]);
Route::post('/store',[
'as'=> 'menus.store',
'uses' => 'MenuController@store'
]);
Route::get('/edit/{id}',[
'as'=> 'menus.edit',
'uses' => 'MenuController@edit'
]);
Route::post('/update/{id}',[
'as'=> 'menus.update',
'uses' => 'MenuController@update'
]);
Route::get('/delete/{id}',[
'as'=> 'menus.delete',
'uses' => 'MenuController@delete'
]);
});
//Product
Route::prefix('product')->group(function () {
Route::get('/',[
'as'=> 'product.index',
'uses' => 'AdminProductController@index'
]);

})
;
});


-    Vào sider.blade.php
<!-- Main Sidebar Container -->
<aside class="main-sidebar sidebar-dark-primary elevation-4">
<!-- 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>
<div class="info">
<a href="#" class="d-block">Alexander Pierce</a>
</div>
</div>

<!-- SidebarSearch Form -->
<div class="form-inline">
<div class="input-group" data-widget="sidebar-search">
<input class="form-control form-control-sidebar" type="search" placeholder="Search" aria-label="Search">
<div class="input-group-append">
<button class="btn btn-sidebar">
<i class="fas fa-search fa-fw"></i>
</button>
</div>
</div>
</div>

<!-- Sidebar Menu -->
<nav class="mt-2">
<ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
<!-- Add icons to the links using the .nav-icon class
with font-awesome or any other icon font library -->

<li class="nav-item">
<a href="{{ route('categories.index') }}" class="nav-link">
<i class="nav-icon fas fa-th"></i>
<p>
Danh mc sn phm
<span class="right badge badge-danger">New</span>
</p>
</a>
</li>

<li class="nav-item">
<a href="{{ route('menus.index') }}" class="nav-link">
<i class="nav-icon fas fa-th"></i>
<p>
Menus
</p>
</a>
</li>

<li class="nav-item">
<a href="{{ route('product.index') }}" class="nav-link">
<i class="nav-icon fas fa-th"></i>
<p>
Sn phm
</p>
</a>
</li>

</ul>
</nav>
<!-- /.sidebar-menu -->
</div>
<!-- /.sidebar -->
</aside>
-     Vào teminal chạy lệnh: php artisan make:controller AdminProductController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AdminProductController extends Controller
{
public function index()
{
return view('admin.product.index');
}
}
-    Vào admin => tạo foder product => tạo file index.blade.php
<!-- Stored in resources/views/child.blade.php -->

@extends('layouts.admin')

@section('title')
<title>Add Product</title>
@endsection

@section('content')
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
@include('partials.content-header',['name'=>'product', 'key'=>'List'])
<!-- /.content-header -->

<!-- Main content -->
<div class="content">
<div class="container-fluid">
<div class="row">
<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">Tên sn phm</th>
<th scope="col">Giá</th>
<th scope="col">Hình nh</th>
<th scope="col">Danh mc</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>

{{-- @foreach($categories as $category)--}}

<tr>
<th scope="row">1</th>
<td>iphone 4</td>
<td>2.400.000</td>
<td>
<img src="" alt="">
</td>
<td>Đin thoi</td>
<td>
<a href=""
class="btn btn-default">Edit</a>
<a href=""
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

Đăng nhận xét

0 Nhận xét

myadcash