Xử lý định dạng date người dùng đưa fomat m/d/Y ghi vào db là Y-m-d như nào? (ok)

https://www.itsolutionstuff.com/post/laravel-9-eloquent-mutators-and-accessors-exampleexample.html

C:\xampp\htdocs\test\database\migrations\2014_10_12_000000_create_users_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration {
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up() {
    Schema::create('users', function (Blueprint $table) {
      $table->id();
      $table->string('name');
      $table->string('email')->unique();
      $table->timestamp('email_verified_at')->nullable();
      $table->string('password');
      $table->rememberToken();
      $table->date('date_of_birth');
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down() {
    Schema::dropIfExists('users');
  }
}

C:\xampp\htdocs\test\app\Models\User.php

<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable {
  use HasApiTokens, HasFactory, Notifiable;
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = [
    'name',
    'email',
    'password',
    'date_of_birth',
  ];
  /**
   * The attributes that should be hidden for serialization.
   *
   * @var array
   */
  protected $hidden = [
    'password',
    'remember_token',
  ];
  /**
   * The attributes that should be cast.
   *
   * @var array
   */
  protected $casts = [
    'email_verified_at' => 'datetime',
  ];
  /**
   * Interact with the user's first name.
   *
   * @param  string  $value
   * @return \Illuminate\Database\Eloquent\Casts\Attribute
   */
  protected function dateOfBirth(): Attribute {
    return new Attribute(
      fn($value) => Carbon::parse($value)->format('m/d/Y'), // get
      fn($value) => Carbon::parse($value)->format('Y-m-d'), // set
    );
  }
}

C:\xampp\htdocs\test\app\Http\Controllers\UserController.php

<?php
namespace App\Http\Controllers;
use App\Models\User;
class UserController extends Controller {
  /**
   * Write code on Method
   *
   * @return response()
   */
  public function create() {
    $input = [
      'name'          => 'Hardik',
      'email'         => 'hardik3@gmail.com',
      'password'      => bcrypt('123456'),
      'date_of_birth' => '07/21/1994',
    ];
    $user = User::create($input);
    dd($user);
  }
  /**
   * Write code on Method
   *
   * @return response()
   */
  public function show() {
    $user = User::first();
    dd($user->toArray());
  }
}

C:\xampp\htdocs\test\routes\web.php

<?php
use App\Http\Controllers\UserController;
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::controller(UserController::class)->group(function () {
  Route::get('create-user', 'create');
  Route::get('get-user', 'show');
});

Last updated