<repo_name>nguyenminhtoan99/owlfashion<file_sep>/app/Http/Controllers/auth/AdminController.php
<?php

namespace App\Http\Controllers\auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\auth\LoginRequest;
use App\Model\Admin;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;




class AdminController extends Controller
{


    public function getLogin(){
        return view('admin.auth.login');
    }

    public function postLogin(LoginRequest $request){
        $admin = Admin::query()
            ->where('email', $request->get('email'))
            ->first();

        if ($admin !== null) {
            if (Hash::check($request->get('password'), $admin->password)) {
               // $request->session()->put('login', $request->input('email'));
               $request->session()->put('login', true);
			    return redirect('/admin/category/');
            }
            return back()->with('notification', 'Sai email hoặc mật khẩu')->withInput();
        }
        return back()->with('notification', 'Sai email hoặc mật khẩu')->withInput();
    }

    public function logout()
    {
        Auth::logout();
        Session::forget('login');
        //$request->session()->forget('login');
        return redirect("login");
    }
}
<file_sep>/app/Http/Controllers/admin/BrandController.php
<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\BrandRequest;
use App\models\Brand;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Support\Str;

class BrandController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $brand = Brand::all();
        return view('admin.pages.brand.index')->with('brands', $brand);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.pages.brand.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(BrandRequest $request)
    {
        $data = $request->all();
        $slug = Str::slug($request->name);
        $data['slug'] = $slug;
        $status = Brand::create($data);

        if ($status) {
            Toastr::success('Đã thêm thành công nha san xuat', 'Thông báo');
        } else {
            Toastr::error('Xảy ra lỗi, Vui lòng thử lại!', 'Thông báo');
        }
        return redirect()->route('brand.index');
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $brand = Brand::findOrFail($id);
        return view('admin.pages.brand.edit')->with('brand', $brand);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(BrandRequest $request, $id)
    {
        $brand = Brand::findOrFail($id);
        $status = $brand->update($request->all());
        if ($status) {
            Toastr::success('Đã sửa thành công nha san xuat', 'Thông báo');
        } else {
            Toastr::error('Xảy ra lỗi, Vui lòng thử lại!', 'Thông báo');
        }
        return redirect()->route('category.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $brand = Brand::findOrFail($id);
        $status = $brand->delete();
        if ($status) {
            Toastr::success('Đã xoá thành công nha san xuat', 'Thông báo');
        } else {
            Toastr::error('Xảy ra lỗi, Vui lòng thử lại!', 'Thông báo');
        }
        return redirect()->route('brand.index');
    }
}
<file_sep>/routes/web.php
<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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('/', function () {
    return view('welcome');
});


Route::get('login', 'auth\AdminController@getLogin')->name('admin.login');
Route::post('login', 'auth\AdminController@postLogin')->name('admin.login');
Route::get('logout', 'auth\AdminController@logout')->name('admin.logout');;

Route::group(['prefix'=>'/admin','middleware' => ['login']], function () {
    //DANH MUC
    Route::get('category/create','admin\CategoryController@create')->name('category.create');
    Route::post('category/create','admin\CategoryController@store')->name('category.store');
    Route::get('category/','admin\CategoryController@index')->name('category.index');
    Route::get('category/edit/{id}','admin\CategoryController@edit')->name('category.edit');
    Route::put('category/update/{id}','admin\CategoryController@update')->name('category.update');
    Route::get('category/delete/{id}','admin\CategoryController@destroy')->name('category.destroy');

     //NHA SAN XUAT
     Route::get('brand/create','admin\BrandController@create')->name('brand.create');
     Route::post('brand/create','admin\BrandController@store')->name('brand.store');
     Route::get('brand/','admin\BrandController@index')->name('brand.index');
     Route::get('brand/edit/{id}','admin\BrandController@edit')->name('brand.edit');
     Route::put('brand/update/{id}','admin\BrandController@update')->name('brand.update');
     Route::get('brand/delete/{id}','admin\BrandController@destroy')->name('brand.destroy');

     //SAN PHAM
     Route::get('product/create','admin\ProductController@create')->name('product.create');
     Route::post('product/create','admin\ProductController@store')->name('product.store');
     Route::get('product/','admin\ProductController@index')->name('product.index');
     Route::get('product/edit/{id}','admin\ProductController@edit')->name('product.edit');
     Route::put('product/update/{id}','admin\ProductController@update')->name('product.update');
     Route::get('product/delete/{id}','admin\ProductController@destroy')->name('product.destroy');
});


<file_sep>/app/models/Product.php
<?php

namespace App\models;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = 'products';

    protected $fillable=['name','slug','description','cat_id','price','brand_id','discount','status','photo','size','quantity','condition'];


    public function categories()
    {
        return $this->belongsto('App\models\Category','cat_id','id');
    }

    public function brands(){
        return $this->belongsTo('App\models\Brand','brand_id','id');
    }

    public $timestamps = true;
}
<file_sep>/app/Http/Controllers/admin/ProductController.php
<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\models\Brand;
use App\models\Category;
use App\models\Product;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Support\Str;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $products=Product::all();
        return view('admin.pages.product.index')->with('products',$products);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = Category::get();
        $brands=Brand::get();
        return view('admin.pages.product.create')
        ->with('categories', $categories)
        ->with('brands', $brands);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // $this->validate($request,[
        //     'name'=>'string|required',
        //     'description'=>'string|nullable',
        //     'photo'=>'string|required',
        //     'size'=>'nullable',
        //     'quantity'=>"required|numeric",
        //     'cat_id'=>'required|exists:categories,id',
        //     'brand_id'=>'nullable|exists:brands,id',
        //     'status'=>'required|in:active,inactive',
        //     'condition'=>'required|in:default,new,hot',
        //     'price'=>'required|numeric',
        //     'discount'=>'nullable|numeric'
        // ]);

        $data=$request->all();
        $slug=Str::slug($request->name);
        $data['slug']=$slug;
        $size=$request->input('size');
        if($size){
            $data['size']=implode(',',$size);
        }
        else{
            $data['size']='';
        }
        if($request->hasFile('photo')){
            $image=$request->file('photo');
            if($image->isValid()){
                $fileName=time() . "_" . rand(0,9999999) . "." .$image->getClientOriginalExtension();
                $image->move(public_path('products'), $fileName);
                $data['photo']=$fileName;
            }
        }
        $status=Product::create($data);
        if($status){
            Toastr::success('Đã thêm thành công sản phẩm', 'Thông báo');
        }
        else{
            Toastr::error('Xảy ra lỗi, Vui lòng thử lại!', 'Thông báo');
        }
        return redirect()->route('product.index');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $product=Product::findOrFail($id);
        return view('admin.pages.product.edit')->with('product',$product);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $product= Product::findOrFail($id);
        $status = $product->update($request->all());
        if($status){
            Toastr::success('Đã sửa thành công sản phẩm', 'Thông báo');
        }
        else{
            Toastr::error('Xảy ra lỗi, Vui lòng thử lại!', 'Thông báo');
        }
        return redirect()->route('product.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $product=Product::findOrFail($id);
        $status=$product->delete();
        if($status){
            Toastr::success('Đã xoá thành công sản phẩm', 'Thông báo');
        }
        else{
            Toastr::error('Xảy ra lỗi, Vui lòng thử lại!', 'Thông báo');
        }
        return redirect()->route('product.index');
    }
}
