Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PhoeniX5's avatar

Can't insert date in varchar Mysql table!

Hello, I have an issue when trying to insert data in MySQL table, here is my code :

dd($request->produit_designation.' '.date('d-m-Y'));
Produit::firstOrCreate(['produit_designation' => $request->produit_designation], ['produit_designation' => $request->produit_designation.'  '.date('d-m-Y'));

when dd it shows the requested value + the current date but after inserting it only inserts the value without the date ?

0 likes
17 replies
automica's avatar

@phoenix5 this

Produit::firstOrCreate(['produit_designation' => $request->produit_designation], ['produit_designation' => $request->produit_designation.'  '.date('d-m-Y'));

is missing a closing square bracket

Produit::firstOrCreate(
    ['produit_designation' => $request->produit_designation],
    ['produit_designation' => $request->produit_designation . '  ' . date('d-m-Y')]
);
MichalOravec's avatar

You use firstOrCreate. If you posted a form where $request->produit_designation exists in the database it will be never inserted because it just return your product from database.

Byt the way use only English for everything when you write a code.

PhoeniX5's avatar

@michaloravec sorry if I was not clear enought, the problem is not that the record is not inserted in the database, all the records are inserted but without the date when :

dd($request->product_designation . ' ' . date ('d-m-Y'));

xxxxx 29-09-2020

but when inserting the record in the database, it only inserts xxxxx!

MichalOravec's avatar

How I said, try it just with create and you will see it in the database.

Produit::create([
    'produit_designation' => $request->produit_designation.' '.date('d-m-Y')
]);
PhoeniX5's avatar

I get this message :

message: "SQLSTATE[HY000]: General error: 1364 Field 'produit_designation' doesn't have a default value (SQL: insert into `Produit` (`updated_at`, `created_at`) values (2020-09-29 08:32:06, 2020-09-29 08:32:06))"
PhoeniX5's avatar

I already have it :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Produit extends Model
{
    protected $fillable = [
        'produit_id', 'produit_designation',
    ];
}

MichalOravec's avatar

And do you have timestamps in migration for product?

If not, you have to say to model that you don't use it

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Produit extends Model
{
   /**
    * Indicates if the model should be timestamped.
    *
    * @var boolean
    */
    public $timestamps = false;

    protected $fillable = [
        'produit_id', 'produit_designation',
    ];
}
Snapey's avatar

Show your code to create the record, you must have copied the example wrong?

PhoeniX5's avatar

Here is my code :

function Creation_production(Request $request)
    {
       ...
        $produit = Produit::where('produit_designation', $request->produit)->first();
        Production::firstOrCreate(
            ['produit_id' => $produit->produit_id, 'production_designation' => $request->production_designation],
            ['produit_id' => $produit->produit_id, 'production_designation' => $request->production_designation . '  ' . date('d-m-Y'), 'produit_qte' => $request->produit_qte, 'object_day' => $request->object_day]
        );
        $success_output = 'Production ajouté';
        $output = array(
            'error'     =>  $error_array,
            'success'   =>  $success_output
        );
        echo json_encode($output);
    }

The only issue is that the $request->production_designation . ' ' . date('d-m-Y') is running like $request->production_designation the date is not being inserted!

automica's avatar

this is my favorite:

$produit_id->produit_id,

should be

        $produit = DB::table('Produits')->where('produit_designation', $request->produit)->first();

and then

$produit->produit_id,
PhoeniX5's avatar

It's weird issue, can't see what I'm doing wrong ?

automica's avatar
automica
Best Answer
Level 54

@phoenix5 whats the column type you are saving your 'production_designation' into? is the data being concatenated?

if you change your code to:

Production::firstOrCreate(
    [
        'produit_id' => $produit->produit_id,
        'production_designation' => $request->production_designation
    ],
    [
        'produit_id' => $produit->produit_id,
        'production_designation' => date('d-m-Y'),
        'produit_qte' => $request->produit_qte,
        'object_day' => $request->object_day
    ]
);

does the field save?

PhoeniX5's avatar

Thanks, I figured out that in order to work, the arguments checked must be the same as those created, like this :

Production::firstOrCreate(
    [
        'produit_id' => $produit->produit_id,
        'production_designation' => $request->production_designation . ' ' .date('d-m-Y')
    ],
    [
        'produit_id' => $produit->produit_id,
        'production_designation' => $request->production_designation . ' ' .date('d-m-Y'),
        'produit_qte' => $request->produit_qte,
        'object_day' => $request->object_day
    ]
);
1 like
MichalOravec's avatar

@phoenix5 I said that you everytime just retrieve data from database.

By the way your code is so bad.

Please or to participate in this conversation.