By GokiSoft.com| 15:59 07/07/2021|
Học Laravel

Tìm hiểu paginate trong laravel - Quản lý sản phẩm - Lập trình Laravel

Yêu cầu

1. Sử dụng migration tạo bảng product gồm các trường sau : tên sản phẩm, giá tiền, giảm giá, mô tả

2. Sử dụng seeder tạo ra 50 bản ghi cho bảng product

3. Tạo file route đặt tên là product.php -> thực hiên đăng ký trong laravel. File product được cấu hình như sau

<?php

Route::group(['prefix' => '/product'], function () {
		Route::get('/view', 'ProductController@showAll')->name('showAll');
	});


Tạo controller tương ứng và các hàm trong controller để chương trình ko bị lỗi. trong hàm showAll của ProductController thực hiện lấy tất cả dữ liệu trong bảng product. Thực hiện hiển thị dữ liệu ra view index.blade.php. Thực hiên phân trang, mỗi trang gồm 10 sản phầm.

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Phí Văn Long [T1907A]
Phí Văn Long

2020-07-06 03:19:40

product.php


<?php

Route::group(['prefix' => '/products'], function () {
		Route::get('index', 'ProductController@showAll')->name('_showAll');
	});
productController


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
class ProductController extends Controller
{
   public function showAll(Request $request){
   	 $ProductList = DB::table('product')->paginate(10);
    	// $ProductList = DB::table('product')->get();
    	return view('products.index')->with([
   		'ProductList'=>$ProductList
   	]);
}
}

index.blade.php



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>ProductName</th>
                    <th>Price</th>
                    <th>Discount</th>
                    <th>Discription</th>
                    <th>Title</th>
                </tr>
            </thead>
            <tbody>
                @foreach($ProductList as $item)
                    <tr>
                        <td>{{ $item->id}}</td>
                        <td>{{ $item->nameproduct}}</td>
                        <td>{{ $item->link}}</td>
                        <td>{{ $item->price}}</td>
                        <td>{{ $item->installment}}</td>
                        <td>{{ $item->title}}</td>                    
                    </tr>              
                @endforeach
            </tbody>
        </table>
        {!! $ProductList->links() !!} 
</body>
</html>

migration
<?php

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

 class CreateProductTable extends Migration
 {
    /**
     * Run the migrations.
     *
      * @return void
     */
     public function up()
     {
         Schema::create('product', function (Blueprint $table) {
             $table->increments('id');
             $table->string('productname');
             $table->float('price');
             $table->int('discount');
             $table->string('productname');
             $table->string('title');
         });
     }

    /**
     * Reverse the migrations.
     *
      * @return void
     */
     public function down()
     {
         Schema::dropIfExists('product');
     }
 }
productSeeder


<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class ProductSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
    	$this->call(ProductSeeder::class);
        DB::table('product')->insert([
            'nameproduct' => Str::random(10),
            'link' => Str::random(100),
            'price' => Str::random(100000),
            'installment' => Str::random(10),
            'title' => Str::random(100),
        ]);
        factory(App\User::class, 50)->create();
    }
}



thienphu [T1907A]
thienphu

2020-07-05 13:46:37


#product.php


<?php

Route::group(['prefix' => '/product'], function () {
        Route::get('/view', 'ProductController@showAll')->name('showAll');
        Route::get('/input', 'ProductController@input')->name('input');
        Route::post('post', 'ProductController@doPostUser')->name('doPost');
	});


#ProductController.php


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class ProductController extends Controller
{
    public function showAll(REQUEST $REQUEST){
        $userList = DB::table('product')->paginate(10); 
        return view('product.show')->with([
            'userList'=> $userList
        ]);
    }
    public function input(REQUEST $REQUEST){
        return view('product.input');
    }
    public function doPostUser(REQUEST $request){
        
        $productName = $request->ProductName;
        $price = $request->Price;
        $discount = $request->Discount;
        $Description = $request->Description;
      //  echo $productName.$price.$discount.$Description;

        DB::table('product')->insert([
            'productname'=>$productName,
            'price'=>$price,
            'discount'=>$discount,
            'descrition'=>$Description,
        ]);
        //insert xogn se dieu huong = lenh nay
       return redirect()->route('showAll');
    }
}


#input.blade.php


<!DOCTYPE html>

<html>
<head>
	<title></title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

	<div class="container">
		<h1><center>Resgister Product</center></h1>
		<form action="{{ route('doPost') }}" method="POST">

			

     <div class="form-group">
       <label for="ProductName">ProductName</label>
       <!-- chú ý chỗ này lỗi 419 -->
       {{ csrf_field()}} 
       <input type="text" class="form-control" name="ProductName" id="ProductName">
     </div>

     <div class="form-group">
       <label for="Price">Price</label>
       <input type="text" class="form-control" name="Price" id="Price">
     </div>
     <div class="form-group">
      <label for="Discount">Discount</label>
      <input type="text" class="form-control" name="Discount" id="Discount">
    </div>
    <div class="form-group">
      <label for="pwd">Description</label>
      <input type="text" class="form-control" name="Description" id="Description">
    </div>
    
 


    <button  type="submit" id="insertData" class="btn btn-default">Register</button>
  </form>
  <p id="showdata"></p>
</div>


</body>
</html>


#show.blade.php


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>ProductName</th>
                    <th>Price</th>
                    <th>Discount</th>
                    <th>Discription</th>
                    
                </tr>
            </thead>
            <tbody>
                @foreach($userList as $item)
                    <tr>
                        <td>{{ $item->id}}</td>
                        <td>{{ $item->productname}}</td>
                        <td>{{ $item->price}}</td>
                        <td>{{ $item->discount}}</td>
                        <td>{{ $item->descrition}}</td>
                        
                        
                    </tr>
                
                @endforeach
            </tbody>
        </table>
        <!-- câu lệnh phân trang -->
        {!! $userList->links() !!} 
    
</body>
</html>


#2020_07_04_032943_create_product_table.php


<?php

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

class CreateProductTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('product', function (Blueprint $table) {
            $table->id();
            $table->string('productname',100);
            $table->string('price',50);
            $table->string('discount',50);
            $table->string('descrition',150);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('product');
    }
}


#productSeeder.php


<?php

use Illuminate\Database\Seeder;

class productSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // DB::table('product')->DB::insert([
        //     ['productname'=>'bcs1','price'=>'100$','discount'=>'1%','descrition'=>'very good'],
        //     ['productname'=>'bcs1','price'=>'100$','discount'=>'1%','descrition'=>'very good'],
        //     ['productname'=>'bcs1','price'=>'100$','discount'=>'1%','descrition'=>'very good'],
        // ]);
        DB::table('product')->insert([
        	'productname'=>'Nokia 1280',
        	'price'=>'100$',
            'discount'=>'1%',
            'descrition'=>'very good'
        ]);
        DB::table('product')->insert([
        	['productname'=>'Nokia 1280','price'=>'100$','discount'=>'1%','descrition'=>'very good'],
            ['productname'=>'Nokia 1280','price'=>'100$','discount'=>'1%','descrition'=>'very good'],
            ['productname'=>'Nokia 1280','price'=>'100$','discount'=>'1%','descrition'=>'very good'],
        ]);
    }
}