samedi 25 mai 2019

Date and timestamp format and unique rule

I have this migration:

   Schema::create('atp_players', function (Blueprint $table) {
        $table->bigIncrements('id');

        $table->string('first_name');
        $table->string('last_name');
        $table->date('birthdate');
        $table->bigInteger('geo_country_id')->unsigned();
        $table->boolean('retired')->default(0);

        $table->softDeletes();
        $table->timestamps();

        $table->foreign('geo_country_id')->references('id')->on('geo_countries')->onDelete('cascade')->onUpdate('cascade');
    });

My first problem is that when I get o Store an instance I receive all fields as strings.

Result of the Postman:

  {
    "first_name": "Tomas",
    "last_name": "berdych",
    "birthdate": "1988-08-10",
    "geo_country_id": "1",
    "updated_at": "2019-05-25 14:13:41",
    "created_at": "2019-05-25 14:13:41",
    "id": 12
  }

Controller:

 public function store(ATPPlayerRequest $request)
  {
    return ATPPlayer::create($request->all());
  }

And my second problem is that the unique Rule not working with format d-m-Y.

 return [
        'first_name' => ['required'],
        'last_name' => ['required'],
        'birthdate' => ['required', 'date','date_format:d-m-Y', 'unique:atp_players,birthdate'],
        'geo_country_id' => ['required','exists:geo_countries,id']
    ];

In the model I use an accesor:

   public function setBirthdateAttribute($value)
   {
    $this->attributes['birthdate'] = date('Y-m-d', strtotime($value));
   }

If I use a mutator to format created_at or deleted_at

$value->format(..) -> this doesnt work because I receive as string instead of timestamp!



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2QqTW3p
via IFTTT

Aucun commentaire:

Enregistrer un commentaire