Download Laravel App
https://laravel.com/docs/11.x/installation
composer global require laravel/installer
C:\xampp\htdocs\laravel11\myapp>composer global require laravel/installer
Connecting our Database
open .env file root directory.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=laravel11dev
DB_USERNAME=root
DB_PASSWORD=root
Install Laravel Breeze
https://laravel.com/docs/11.x/starter-kits#laravel-breeze
composer require laravel/breeze --dev
Database Migration
Edit database/migrations/create_users_table.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //database/migrations/create_users_table.php <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create( 'users' , function (Blueprint $table ) { $table ->id(); $table ->string( 'name' ); $table ->string( 'email' )->unique(); $table ->string( 'usertype' )-> default ( 'user' ); $table ->timestamp( 'email_verified_at' )->nullable(); $table ->string( 'password' ); $table ->rememberToken(); $table ->timestamps(); }); } }; |
C:\xampp\htdocs\laravel\laravel11\myapp>php artisan migrate
Create middleware Admin
php artisan make:middleware Admin
C:\xampp\htdocs\laravel\laravel11\myapp>php artisan make:middleware Admin
app\Http\Middleware\Admin.php add code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | //app\Http\Middleware\Admin.php <?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Symfony\Component\HttpFoundation\Response; class Admin { /** * Handle an incoming request. * * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next */ public function handle(Request $request , Closure $next ): Response { if (Auth::user()->usertype != 'admin' ) { return redirect( 'dashboard' ); } return $next ( $request ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | // bootstrap\app.php <?php use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__. '/../routes/web.php' , commands: __DIR__. '/../routes/console.php' , health: '/up' , ) ->withMiddleware( function (Middleware $middleware ) { $middleware ->alias([ 'admin' => \App\Http\Middleware\Admin:: class , ]); }) ->withExceptions( function (Exceptions $exceptions ) { // })->create(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //app\Http\Auth\AuthenticatedSessionController.php <?php namespace App\Http\Controllers\Auth; class AuthenticatedSessionController extends Controller { /** * Handle an incoming authentication request. */ public function store(LoginRequest $request ): RedirectResponse { $request ->authenticate(); $request ->session()->regenerate(); if ( $request ->user()->usertype == 'admin' ) { return redirect( 'admin/dashboard' ); } //return redirect()->intended(route('dashboard', absolute: false)); return redirect()->intended(route( 'dashboard' )); } } |
php artisan make:controller HomeController
C:\xampp\htdocs\laravel\laravel10project>php artisan make:controller HomeController
app\Http\Controllers\HomeController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //app\Http\Controllers\HomeController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; class HomeController extends Controller { public function index() { return view( 'admin.dashboard' ); } } |
C:\xampp\htdocs\laravel\laravel11\myapp>php artisan make:model Product -mc
database/migrations/create_products_table.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | //database/migrations/create_products_table.php <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create( 'products' , function (Blueprint $table ) { $table ->id(); $table ->string( 'title' ); $table ->string( 'category' ); $table ->integer( 'price' ); $table ->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists( 'products' ); } }; |
app/Models/Product.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // app/Models/Product.php <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Product extends Model { use HasFactory; protected $table = 'products' ; protected $fillable = [ 'title' , 'category' , 'price' , ]; } |
C:\xampp\htdocs\laravel\laravel11\myapp>php artisan migrate
Create Controller
php artisan make:controller ProductController
C:\xampp\htdocs\laravel\laravel10project>php artisan make:controller ProductController
app\Http\Controllers\ProductController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | //app\Http\Controllers\ProductController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Product; class ProductController extends Controller { public function index() { $products = Product::orderBy( 'id' , 'desc' )->get(); $total = Product:: count (); return view( 'admin.product.home' , compact([ 'products' , 'total' ])); } public function create() { return view( 'admin.product.create' ); } public function save(Request $request ) { $validation = $request ->validate([ 'title' => 'required' , 'category' => 'required' , 'price' => 'required' , ]); $data = Product::create( $validation ); if ( $data ) { session()->flash( 'success' , 'Product Add Successfully' ); return redirect(route( 'admin/products' )); } else { session()->flash( 'error' , 'Some problem occure' ); return redirect(route( 'admin.products/create' )); } } public function edit( $id ) { $products = Product::findOrFail( $id ); return view( 'admin.product.update' , compact( 'products' )); } public function delete ( $id ) { $products = Product::findOrFail( $id )-> delete (); if ( $products ) { session()->flash( 'success' , 'Product Deleted Successfully' ); return redirect(route( 'admin/products/' )); } else { session()->flash( 'error' , 'Product Not Delete successfully' ); return redirect(route( 'admin/products/' )); } } public function update(Request $request , $id ) { $products = Product::findOrFail( $id ); $title = $request ->title; $category = $request ->category; $price = $request ->price; $products ->title = $title ; $products ->category = $category ; $products ->price = $price ; $data = $products ->save(); if ( $data ) { session()->flash( 'success' , 'Product Update Successfully' ); return redirect(route( 'admin/products' )); } else { session()->flash( 'error' , 'Some problem occure' ); return redirect(route( 'admin/products/update' )); } } } |
bootstrap cdn https://www.bootstrapcdn.com/
create folder admin new file dashboard.blade.php resources/views/admin/dashboard.blade.php
add bootstrap resources/views/layouts/app.blade.php
https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //resources/views/admin/dashboard.blade.php <x-app-layout> <x-slot name= "header" > <h2 class = "font-semibold text-xl text-gray-800 leading-tight" > {{ __( 'Dashboard Admin' ) }} </h2> </x-slot> <div class = "py-12" > <div class = "max-w-7xl mx-auto sm:px-6 lg:px-8" > <div class = "bg-white overflow-hidden shadow-sm sm:rounded-lg" > <div class = "p-6 text-gray-900" > {{ __( "You're logged in!" ) }} <p><a href= "products" class = "btn btn-primary" >Products</a></p> </div> </div> </div> </div> </x-app-layout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | // resources/views/admin/product/home.blade.php <x-app-layout> <x-slot name= "header" > <h2 class = "font-semibold text-xl text-gray-800 leading-tight" > {{ __( 'Admin Product' ) }} </h2> </x-slot> <div class = "py-12" > <div class = "max-w-7xl mx-auto sm:px-6 lg:px-8" > <div class = "bg-white overflow-hidden shadow-sm sm:rounded-lg" > <div class = "p-6 text-gray-900" > <div class = "d-flex align-items-center justify-content-between" > <h1 class = "mb-0" >List Product</h1> <a href= "{{ route('admin/products/create') }}" class = "btn btn-primary" >Add Product</a> </div> <hr /> @ if (Session::has( 'success' )) <div class = "alert alert-success" role= "alert" > {{ Session::get( 'success' ) }} </div> @ endif <table class = "table table-hover" > <thead class = "table-primary" > <tr> <th>#</th> <th>Title</th> <th>Category</th> <th>Price</th> <th>Action</th> </tr> </thead> <tbody> @forelse ( $products as $product ) <tr> <td class = "align-middle" >{{ $loop ->iteration }}</td> <td class = "align-middle" >{{ $product ->title }}</td> <td class = "align-middle" >{{ $product ->category }}</td> <td class = "align-middle" >{{ $product ->price }}</td> <td class = "align-middle" > <div class = "btn-group" role= "group" aria-label= "Basic example" > <a href= "{{ route('admin/products/edit', ['id'=>$product->id]) }}" type= "button" class = "btn btn-secondary" >Edit</a> <a href= "{{ route('admin/products/delete', ['id'=>$product->id]) }}" type= "button" class = "btn btn-danger" > Delete </a> </div> </td> </tr> @ empty <tr> <td class = "text-center" colspan= "5" >Product not found</td> </tr> @endforelse </tbody> </table> </div> </div> </div> </div> </x-app-layout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | // resources/views/admin/product/create.blade.php <x-app-layout> <x-slot name= "header" > <h2 class = "font-semibold text-xl text-gray-800 leading-tight" > {{ __( 'Create Product' ) }} </h2> </x-slot> <div class = "py-12" > <div class = "max-w-7xl mx-auto sm:px-6 lg:px-8" > <div class = "bg-white overflow-hidden shadow-sm sm:rounded-lg" > <div class = "p-6 text-gray-900" > <h1 class = "mb-0" >Add Product</h1> <hr /> @ if (session()->has( 'error' )) <div> {{session( 'error' )}} </div> @ endif <p><a href= "{{ route('admin/products') }}" class = "btn btn-primary" >Go Back</a></p> <form action= "{{ route('admin/products/save') }}" method= "POST" enctype= "multipart/form-data" > @csrf <div class = "row mb-3" > <div class = "col" > <input type= "text" name= "title" class = "form-control" placeholder= "Title" > @error( 'title' ) <span class = "text-danger" >{{ $message }}</span> @enderror </div> </div> <div class = "row mb-3" > <div class = "col" > <input type= "text" name= "category" class = "form-control" placeholder= "Category" > @error( 'category' ) <span class = "text-danger" >{{ $message }}</span> @enderror </div> </div> <div class = "row mb-3" > <div class = "col" > <input type= "text" name= "price" class = "form-control" placeholder= "Price" > @error( 'price' ) <span class = "text-danger" >{{ $message }}</span> @enderror </div> </div> <div class = "row" > <div class = "d-grid" > <button class = "btn btn-primary" >Submit</button> </div> </div> </form> </div> </div> </div> </div> </x-app-layout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | //resources/views/admin/product/update.blade.php <x-app-layout> <x-slot name= "header" > <h2 class = "font-semibold text-xl text-gray-800 leading-tight" > {{ __( 'Edit Product' ) }} </h2> </x-slot> <div class = "py-12" > <div class = "max-w-7xl mx-auto sm:px-6 lg:px-8" > <div class = "bg-white overflow-hidden shadow-sm sm:rounded-lg" > <div class = "p-6 text-gray-900" > <h1 class = "mb-0" >Edit Product</h1> <hr /> <form action= "{{ route('admin/products/update', $products->id) }}" method= "POST" > @csrf @method( 'PUT' ) <div class = "row" > <div class = "col mb-3" > <label class = "form-label" >Product Name</label> <input type= "text" name= "title" class = "form-control" placeholder= "Title" value= "{{$products->title}}" > @error( 'title' ) <span class = "text-danger" >{{ $message }}</span> @enderror </div> </div> <div class = "row" > <div class = "col mb-3" > <label class = "form-label" >Category</label> <input type= "text" name= "category" class = "form-control" placeholder= "Category" value= "{{$products->category}}" > @error( 'category' ) <span class = "text-danger" >{{ $message }}</span> @enderror </div> </div> <div class = "row" > <div class = "col mb-3" > <label class = "form-label" >Price</label> <input type= "text" name= "price" class = "form-control" placeholder= "Product Price" value= "{{$products->price}}" > @error( 'price' ) <span class = "text-danger" >{{ $message }}</span> @enderror </div> </div> <div class = "row" > <div class = "d-grid" > <button class = "btn btn-warning" >Update</button> </div> </div> </form> </div> </div> </div> </div> </x-app-layout> |
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | //routes/web.php <?php use App\Http\Controllers\ProfileController; use Illuminate\Support\Facades\Route; use App\Http\Controllers\HomeController; use App\Http\Controllers\ProductController; Route::get( '/' , function () { return view( 'welcome' ); }); Route::get( '/dashboard' , function () { return view( 'dashboard' ); })->middleware([ 'auth' , 'verified' ])->name( 'dashboard' ); Route::middleware( 'auth' )->group( function () { Route::get( '/profile' , [ProfileController:: class , 'edit' ])->name( 'profile.edit' ); Route::patch( '/profile' , [ProfileController:: class , 'update' ])->name( 'profile.update' ); Route:: delete ( '/profile' , [ProfileController:: class , 'destroy' ])->name( 'profile.destroy' ); }); Route::middleware([ 'auth' , 'admin' ])->group( function () { Route::get( 'admin/dashboard' , [HomeController:: class , 'index' ]); Route::get( '/admin/products' , [ProductController:: class , 'index' ])->name( 'admin/products' ); Route::get( '/admin/products/create' , [ProductController:: class , 'create' ])->name( 'admin/products/create' ); Route::post( '/admin/products/save' , [ProductController:: class , 'save' ])->name( 'admin/products/save' ); Route::get( '/admin/products/edit/{id}' , [ProductController:: class , 'edit' ])->name( 'admin/products/edit' ); Route::put( '/admin/products/edit/{id}' , [ProductController:: class , 'update' ])->name( 'admin/products/update' ); Route::get( '/admin/products/delete/{id}' , [ProductController:: class , 'delete' ])->name( 'admin/products/delete' ); }); require __DIR__. '/auth.php' ; //Route::get('admin/dashboard', [HomeController::class, 'index']); //Route::get('admin/dashboard', [HomeController::class, 'index'])->middleware(['auth', 'admin']); |
Starting Laravel development server: http://127.0.0.1:8000