Saturday, 15 February 2025

DAY5-Create multiple login users and roles

07 Mar 2024
135
luminox watches

DAY5 กันแล้วนะครับหลังจากที่เราทำ login ไปแล้วเราก็จะมาทำในส่วนของการ login multiple users นะครับเพราะระบบ realestate นั้นจะต้องมี admin มี agent ผู้มาโพสขายบ้านใช่ไหมครับ ดังนั้นเราต้องแก้ไฟล์ migrations กันก่อนนะครับ

 

1.) เพิ่ม field ที่ไฟล์ create_users_table.php ให้มี role admin, agent , users


public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('username')->nullable();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('photo')->nullable();
$table->string('phone')->nullable();
$table->text('address')->nullable();
$table->enum('role',['admin','agent','user'])->default('user');
$table->enum('status',['active','inactive'])->default('active');
$table->rememberToken();
$table->timestamps();
});

DAY5-Create multiple login users and roles

DAY5-Create multiple login users and roles ก่อนแก้ไขไฟล์

 

DAY5-Create multiple login แก้ไขไฟล์ migrations

DAY5-Create multiple login แก้ไขไฟล์ migrations หลังแก้ไขไฟล์

ด้านบนเป็นส่วนของการสร้างตาราง “users” ในฐานข้อมูลของ Laravel โดยใช้การใช้งาน Schema Builder ของ Laravel ซึ่งเป็นวิธีที่สะดวกและสร้างความยืดหยุ่นในการจัดการฐานข้อมูล โดยสร้างตาราง “users” ที่มีคอลัมน์ต่าง ๆ ดังนี้:

id: เป็นคอลัมน์สำหรับ Primary Key ของตาราง
name: ชื่อของผู้ใช้
username: ชื่อผู้ใช้งาน (สามารถเป็นค่าว่างได้)
email: อีเมลของผู้ใช้ (Unique)
email_verified_at: วันที่ทำการยืนยันอีเมล (สามารถเป็นค่าว่างได้)
password: รหัสผ่านของผู้ใช้
photo: รูปภาพของผู้ใช้ (สามารถเป็นค่าว่างได้)
phone: เบอร์โทรศัพท์ของผู้ใช้ (สามารถเป็นค่าว่างได้)
address: ที่อยู่ของผู้ใช้ (สามารถเป็นค่าว่างได้)
role: บทบาทของผู้ใช้ (มีค่าเป็น ‘admin’, ‘agent’, หรือ ‘user’ โดยค่าเริ่มต้นคือ ‘user’)
status: สถานะของผู้ใช้ (มีค่าเป็น ‘active’ หรือ ‘inactive’ โดยค่าเริ่มต้นคือ ‘active’)
remember_token: สำหรับการจำ token เพื่อการยืนยันการใช้งาน
created_at และ updated_at: สำหรับบันทึกวันที่เวลาที่เพิ่มและแก้ไขข้อมูล
โค้ดด้านบนจะสร้างตาราง “users” ในฐานข้อมูลของคุณ โดยมีโครงสร้างและลักษณะที่ระบุไว้ดังกล่าว

 

2. แก้ไขไฟล์ users.php ที่ path => app/modals/user.php ต่อเพื่อป้องกันฟิลด์ที่เราเพิ่มเข้าไปข้างบนทั้งหมด

จากไฟล์เติม protected $fillable = ['name', 'email','password']; <– assign ค่าทีละตัว
เปลี่ยนเป็น protected $guarded =[];
$guarded เป็น property ที่เก็บค่าที่ไม่ต้องการ assign โดยเก็บไว้ใน array
ดังนั้น ถ้าเราปล่อยให้ array เป็นค่าว่าง []; ก็แสดงว่าเรา assign ทุก fields ในตารางนั้นๆ

DAY5-Create multiple login แก้ไขไฟล์ user

DAY5-Create multiple login แก้ไขไฟล์ user

หลังจากแก้ไขเสร็จแล้ว เราก็จะมารันคำสั่ง migrate กันแล้วครับ โดยใช้คำสั่ง ใช้คำสั่ง php artisan migrate

DAY5-Create multiple login ใช้คำสั่ง php artisan migrate

DAY5-Create multiple login ใช้คำสั่ง php artisan migrate

จากนั้นเราก็กลับมาดูที่ database ของเราครับว่าที่เราทำไว้มาใน database หรือยังนะครับ ถ้าได้ตามภาพ เย้! วันนี้คุณสำเร็จไปอีก 1 สเต็ปแล้วจ้าา

DAY5-Create multiple login database after migration

DAY5-Create multiple login database after migration

 

  1. สั่ง php artisan make:seeder UsersTableSeeder
php artisan make:seeder UsersTableSeeder

php artisan make:seeder UsersTableSeeder

new-file-UserstebleSeeder

new-file-UserstebleSeeder

 

เปิด File UsersTableSeeder  copy  use Illuminate\Support\Facades\Hash;  ใส่ลงไป

use Illuminate\Support\Facades\Hash;

use Illuminate\Support\Facades\Hash;

DB::table('users')->insert([
//admin

[
‘name’=>’Admin’,
‘username’=>’admin’,
’email’=>’admin@gmail.com’,
‘password’=> Hash::make(‘111’),
‘role’=>’admin’,
‘status’=>’active’,
],
//agent
[

‘name’=>’Agent’,
‘username’=>’agent’,
’email’=>’agent@gmail.com’,
‘password’=> Hash::make(‘111’),
‘role’=>’agent’,
‘status’=>’active’,
],
//user
[
‘name’=>’User’,
‘username’=>’user’,
’email’=>’user@gmail.com’,
‘password’=> Hash::make(‘111’),
‘role’=>’user’,
‘status’=>’active’,
],
]);
โดยของเดิมมันจะเป็นแบบนี้ครับ

 

 

Thanat Sirikitphattana

แบ่งปันกัน เราอยู่กันไม่เกิน 100 ปีหรอกครับ
สุดท้ายก็ทิ้งไว้ที่โลก จะคงเหลือไว้แต่คุณงามความดีที่ให้ระลึกถึงกันครับ

Follow Us / Thanat Sirikitphattana