# 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)

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');
});
```

![](/files/IZSP8ZrSs16EiHXmS5xI)

![](/files/Jcz5DRk2lvQ4tzmfIvW8)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learnphp.gitbook.io/learnphp/learn-lavarel/xu-ly-dinh-dang-date-nguoi-dung-dua-fomat-m-d-y-ghi-vao-db-la-y-m-d-nhu-nao-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
