ลบอักขระหนึ่งตัวจากด้านหลังของสตริงใน PHP
<?php
$string = "abcdef";
$new_string = substr($string, 0, -1); // ตัดอักขระตัวสุดท้ายออก
echo $new_string; // แสดงผลลัพธ์: abcde
?>
<?php
$string = "abcdef";
$new_string = substr($string, 0, -1); // ตัดอักขระตัวสุดท้ายออก
echo $new_string; // แสดงผลลัพธ์: abcde
?>
<?php
$string = "Hello World";
$lastChar = substr($string, -1);
echo $lastChar; // แสดงผล: d
?>
php artisan make:migration add_column_name_to_table_name_table --table=table_name
เปลี่ยน add_column_name_to_table_name_table เป็นชื่อที่สื่อความหมายถึงการเปลี่ยนแปลง
เปลี่ยน table_name เป็นชื่อตารางที่คุณต้องการแก้ไข
ตัวอย่าง:
ถ้าคุณต้องการเพิ่มคอลัมน์ status ลงในตาราง users:
php artisan make:migration add_status_to_users_table --table=users
แก้ไขไฟล์ Migration
หลังจากรันคำสั่ง จะมีไฟล์ถูกสร้างใน database/migrations/ ให้แก้ไขเนื้อหา:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('status')->default('active'); // เพิ่มคอลัมน์ใหม่
});
}
รัน Migration
php artisan migrate
composer dump-autoload
"autoload": {
"psr-4": {
"App\": "app/",
"Database\Factories\": "database/factories/",
"Database\Seeders\": "database/seeders/"
},
"files": [
"app/Libs/functions.php"
]
}
$request->validate([
'username' => 'required',
'password' => 'required',
], [
'username.required' => 'กรุณากรอกชื่อผู้ใช้',
'password.required' => 'กรุณากรอกรหัสผ่าน',
]);