Workshop นี้เป็นส่วนหนึ่งของคอร์สสอนทำระบบ POS Laravel + PostgreSQL + AdminLTE
- การเชื่อมต่อฐานข้อมูล PostgreSQL ถ้ายังไม่มีโปรแกรมสามารถดาวน์โหลดได้ที่ https://www.postgresql.org/download/
- ไฟล์
.env
ใน Laravel เป็นไฟล์กำหนดค่าต่าง ๆ ของแอปพลิเคชัน โดยเฉพาะค่าที่เปลี่ยนแปลงได้ เช่น การเชื่อมต่อฐานข้อมูล, การตั้งค่าแอป, และ API Key ต่าง ๆ - ตัวอย่างโค้ด
1 2 3 4 5 6 |
DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=pos_db DB_USERNAME=postgres DB_PASSWORD= |
- อธิบายแต่ละบรรทัด
- ระบุประเภทของฐานข้อมูลที่ Laravel จะใช้ (ในที่นี้คือ
pgsql
สำหรับ PostgreSQL) - กำหนดที่อยู่ของเซิร์ฟเวอร์ฐานข้อมูล (
127.0.0.1
หมายถึงเครื่องเดียวกัน หรือlocalhost
) - กำหนดพอร์ตของฐานข้อมูล PostgreSQL (ค่าเริ่มต้นคือ
5432
) - กำหนดชื่อของฐานข้อมูลที่ใช้ (
pos_db
คือชื่อฐานข้อมูล) - ชื่อผู้ใช้ที่ใช้เชื่อมต่อกับฐานข้อมูล (
postgres
เป็นค่าเริ่มต้นของ PostgreSQL) - รหัสผ่านของฐานข้อมูล (ควรตั้งค่าตามที่กำหนดไว้ใน PostgreSQL)
- ระบุประเภทของฐานข้อมูลที่ Laravel จะใช้ (ในที่นี้คือ
- ต่อมา Laravel Migration ที่ใช้สำหรับสร้างตาราง
users
ในฐานข้อมูล PostgreSQL หรือ ฐานข้อมูลอื่น ๆ ที่กำหนดไว้ใน.env
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 |
<?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('users', function (Blueprint $table) { $table->id(); $table->string('username')->unique(); $table->string('password'); $table->string('firstname'); $table->string('lastname'); $table->string('email')->unique(); $table->string('phone')->nullable(); $table->string('role'); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('users'); } }; |
-
- โดยตาราง
users
มีคอลัมน์ดังนี้: - รัน Migration เพื่อสร้างตาราง -> php artisan migrate
- โดยตาราง
- โค้ดนี้เป็น Eloquent Model สำหรับตาราง
users
ใน Laravel โดยใช้คลาสAuthenticatable
ซึ่งช่วยให้โมเดลUser
รองรับระบบ authentication ของ Laravel อัตโนมัติ
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { protected $table = 'users'; protected $primaryKey = 'id'; protected $fillable = ['username', 'password', 'firstname', 'lastname', 'email', 'phone', 'role']; public $timestamps = false; } |
- ระบบล็อกอินและระบบลงทะเบียน (Auth Login and Registration)
- ติดตั้ง package laravel/ui พิมพ์คำสั่งผ่าน Terminal
- composer require laravel/ui
- สร้างหน้าเข้าระบบและหน้าลงทะเบียน พิมพ์คำสั่งผ่าน Terminal
- php artisan ui bootstrap –auth
- ติดตั้ง install npm dependencies โดย run คำสั่ง npm install ผ่าน Terminal
- npm install หรือ npm i
- รันคำสั่ง npm run build ผ่าน Terminal เพื่อ compile CSS
- ติดตั้ง package laravel/ui พิมพ์คำสั่งผ่าน Terminal
- AuthController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class AuthController extends Controller { public function login(Request $request) { if (auth()->attempt(['username' => $request->username, 'password' => $request->password])) { $users = auth()->user(); if ($users->role === 'admin') { return redirect('admin'); } else { return redirect()->route('home'); } } else { return redirect()->back(); } } } |
- register.blade.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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">{{ __('Register') }}</div> <div class="card-body"> <form method="POST" action="{{ route('register') }}"> @csrf <div class="row mb-3"> <label for="username" class="col-md-4 col-form-label text-md-end">{{ __('Username') }}</label> <div class="col-md-6"> <input id="username" type="text" class="form-control @error('username') is-invalid @enderror" name="username" value="{{ old('username') }}" required autocomplete="username" autofocus> @error('username') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="firstname" class="col-md-4 col-form-label text-md-end">{{ __('First Name') }}</label> <div class="col-md-6"> <input id="firstname" type="text" class="form-control @error('firstname') is-invalid @enderror" name="firstname" value="{{ old('firstname') }}" required autocomplete="firstname"> @error('firstname') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="lastname" class="col-md-4 col-form-label text-md-end">{{ __('Last Name') }}</label> <div class="col-md-6"> <input id="lastname" type="text" class="form-control @error('lastname') is-invalid @enderror" name="lastname" value="{{ old('lastname') }}" required autocomplete="lastname"> @error('lastname') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="email" class="col-md-4 col-form-label text-md-end">{{ __('Email Address') }}</label> <div class="col-md-6"> <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email"> @error('email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="phone" class="col-md-4 col-form-label text-md-end">{{ __('Phone') }}</label> <div class="col-md-6"> <input id="phone" type="text" class="form-control @error('phone') is-invalid @enderror" name="phone" value="{{ old('phone') }}" required autocomplete="phone"> @error('phone') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="role" class="col-md-4 col-form-label text-md-end">{{ __('Role') }}</label> <div class="col-md-6"> <select id="role" class="form-control @error('role') is-invalid @enderror" name="role" required> <option value="admin">{{ __('Admin') }}</option> <option value="user">{{ __('User') }}</option> <!-- Add more roles as needed --> </select> @error('role') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label> <div class="col-md-6"> <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password"> @error('password') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> <div class="row mb-3"> <label for="password-confirm" class="col-md-4 col-form-label text-md-end">{{ __('Confirm Password') }}</label> <div class="col-md-6"> <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password"> </div> </div> <div class="row mb-0"> <div class="col-md-6 offset-md-4"> <button type="submit" class="btn btn-primary"> {{ __('Register') }} </button> </div> </div> </form> </div> </div> </div> </div> </div> @endsection |

สนใจจ้างพัฒนาระบบ เขียนระบบ
ทำเว็บไซต์ รับสอนเขียนโปรแกรม
inbox มาที่เพจ หรือติดต่อ 098-373-8651
ช่องทางการชำระเงิน
เงินสด หรือ e-banking
ธนาคารกสิกรไทย
เลขบัญชี : 0951168564
ชื่อบัญชี : นายวัยวุฒิ ชุมเมืองปัก