Daniel Chocano
Daniel Chocano

Reputation: 11

Laravel date is not inserting, update in database table

insert and edit the registry but no date columns. |

programming seemingly fine, but failed to get the date field is properly registered.

use UTF8 encoding, locale is (Es)

why? T_T

Table

Schema::create('products', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('subclassproduct_id')->unsigned();
        $table->integer('branch_id')->unsigned();
        $table->integer('coin_id')->unsigned();
        $table->string('code', 50);
        $table->string('name', 100);
        $table->string('description', 200);
        $table->text('observation');
        $table->decimal('price', 5,2);
        $table->date('validity_since');
        $table->date('validity_until');
        $table->enum('state', ['Activo', 'Desactivado']); 
        $table->softDeletes();
        $table->timestamps();

        $table->foreign('subclassproduct_id')
            ->references('id')->on('subclasses_products')
            ->onUpdate('CASCADE')
            ->onDelete('CASCADE');  

        $table->foreign('branch_id')
            ->references('id')->on('branches')
            ->onUpdate('CASCADE')
            ->onDelete('CASCADE');

        $table->foreign('coin_id')
            ->references('id')->on('coins')
            ->onUpdate('CASCADE')
            ->onDelete('CASCADE');
    });

Model

this is the model of the table

class Product extends Model {
use  SoftDeletes; // for soft delete

protected $table = 'products';

protected $fillable = [
    'subclassproduct_id', 'branch_id', 'coin_id', 'code' ,'name', 'description', 'observation', 'price', 'validity_since', 'validity_until', 'state'
];

protected $dates = ['deleted_at'];   }

Controller

this is the Controller of the table

    public function update(EditProductRequest $request, $id)
{
    $Product = Product::findOrFail($id);
    $Product->fill($request->all());

    $request->merge(array('validity_since' => Carbon::createFromFormat('d/m/Y', $request->get('validity_since') )->toDateString()));
    $request->merge(array('validity_until' => Carbon::createFromFormat('d/m/Y', $request->get('validity_until') )->toDateString()));



    $Product->save();

    return redirect()->route('administrar.products.index');
}

Upvotes: 1

Views: 1086

Answers (1)

The Alpha
The Alpha

Reputation: 146269

Move these lines before fill:

$request->merge(...);
$request->merge(...);

Then use fill and rest of the code:

$Product->fill($request->all());
$Product->save();

// refirect or whatever...

BTW, you may use single merge with an array:

$request->merge([
    'validity_since' => 'value',
    'validity_until' => 'value'
]);

Upvotes: 1

Related Questions