PHP Laravel Frameword – Xây dựng trang website Fresh fruits shop

A. NỘI DUNG

1. MÔ TẢ ĐỀ TÀI

MỤC TIÊU: Xây dựng một website bán hàng về Fresh fruits shop và sưu tầm những loại Fresh fruits shop mà chúng ta vẫn chưa từng được thấy và nhằm cung cấp những thông tin về Fresh fruits hoặc để tặng quà cho người thân hoặc người mình thương

2. CÔNG NGHỆ SỬ DỤNG

  • Laravel: Framework PHP mạnh mẽ và linh hoạt, giúp xây dựng các ứng dụng web nhanh chóng với cấu trúc MVC (Model-View-Controller) rõ ràng.
  • MySQL: Hệ quản trị cơ sở dữ liệu quan hệ phổ biến, dễ sử dụng và có hiệu suất cao.
  • HTML/CSS/JavaScript: Các công nghệ front-end để xây dựng giao diện người dùng.
  • Blade: Template engine của Laravel để tạo giao diện người dùng động.

3. CHỨC NĂNG CHÍNH

QUẢN LÝ SẢN PHẨM

  • Thêm, sửa, xóa sản phẩm (dành cho admin).

QUẢN LÝ ĐƠN HÀNG

  • Quản lý trạng thái đơn hàng (admin).

4. THIẾT KẾ CƠ SỞ DỮ LIỆU

  • Bảng users: Lưu thông tin người dùng (id, name, email, password, role, created_at, updated_at).
  • Bảng products: Lưu thông tin sản phẩm (id, name, description, price, category_id, created_at, updated_at).
  • Bảng categories: Lưu thông tin danh mục sản phẩm (id, name, created_at, updated_at).
  • Bảng orders: Lưu thông tin đơn hàng (id, user_id, code, status, created_at, updated_at).
  • Bảng order_items: Lưu thông tin chi tiết đơn hàng (id, order_id, product_id, quantity, price, created_at, updated_at).

5. MÔ HÌNH DỮ LIỆU WORKBENCH

6. MODEL

Category

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasFactory;

    protected $fillable = ['name','desc'];

    public function products(){
        return $this->hasMany(Product::class);
    }
}

Order

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    use HasFactory;

    public function OrderItems(){
        return $this->hasMany(OrderItem::class);
    }

    public function users(){
        return $this->belongsTo(User::class);
    }
}

OrderItem

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderItem extends Model
{
    use HasFactory;

    public function products(){
        return $this->belongsTo(Product::class);
    }

    public function orders(){
        return $this->belongsTo(Order::class);
    }
}

Product

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;
    
    public function Category(){
        return $this->belongsTo(Category::class);
    }

    public function OrderItems(){
        return $this->hasMany(OrderItem::class);
    }
}

User

<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
    ];

    public function orders(){
        return $this->hasMany(Order::class);
    }
}

7. CONTROLLER

7.1. ADMIN

UserController

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     * 
     * @return \Illuminate\Response
     */
    public function index()
    {
        $userList = User::all();
        return view('admin.users.index',['userList' => $userList]) ;
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
       return view('admin.users.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $user = User::create($request->only([
            'name', 'email', 'password'
        ]));
        $message = "Success full create";
        if($user == null){
            $message = "Success full failed";
        }
        return redirect()->route('admin.users.index')->with('massage', $message);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $user = User::findOrFail($id);
        return view('admin.users.edit', compact('user'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $user = User::findOrFail($id);
        $bool = $user->update($request->only([
            'name', 'email', 'password'
        ]));
        $message = "Success update";
        if( $bool){
            $message = "Update failed ";
        }
        return redirect()->route('admin.users.index')->with('massage', $message);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        $message = "Success delete";
        if(!User::destroy($id)){
            $message = "Delete failed";
        }
        return redirect()->route('admin.users.index')->with('massage', $message);
    }
}

ProductController

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $productList = Product::all();
        return view('admin.products.index',['productList' => $productList]) ;
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('admin.products.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $product = Product::create($request->only([
            'name', 'email', 'password'
        ]));
        $message = "Success full create";
        if($product == null){
            $message = "Success full failed";
        }
        return redirect()->route('admin.prducts.index')->with('massage', $message);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $product = Product::findOrFail($id);
        return view('admin.products.edit', compact('product'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $product = Product::findOrFail($id);
        $bool = $product->update($request->only([
            'name', 'email', 'password'
        ]));
        $message = "Success update";
        if( $bool){
            $message = "Update failed ";
        }
        return redirect()->route('admin.products.index')->with('massage', $message);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        $message = "Success delete";
        if(!Product::destroy($id)){
            $message = "Delete failed";
        }
        return redirect()->route('admin.products.index')->with('massage', $message);
    }
}

OrderController

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Order;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $orderList = Order::all();
        return view('admin.orders.index',['orderList' => $orderList]) ;
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('admin.orders.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $order = Order::create($request->only([
            'name', 'email', 'password'
        ]));
        $message = "Success full create";
        if($order == null){
            $message = "Success full failed";
        }
        return redirect()->route('admin.orders.index')->with('massage', $message);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        $order = Order::findOrFail($id);
        return view('admin.orders.edit', compact('order'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $order = Order::findOrFail($id);
        $bool = $order->update($request->only([
            'name', 'email', 'password'
        ]));
        $message = "Success update";
        if( $bool){
            $message = "Update failed ";
        }
        return redirect()->route('admin.orders.index')->with('massage', $message);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        $message = "Success delete";
        if(!Order::destroy($id)){
            $message = "Delete failed";
        }
        return redirect()->route('admin.orders.index')->with('massage', $message);
    }
}
https://github.com/ThanhGiau2005/eshop.

Thành Viên

Phan Thanh Giàu

Phạm Nhật Quyền

Dương Phước Nhật Tiến