【问题标题】:Laravel date is not inserting, update in database tableLaravel 日期未插入,在数据库表中更新
【发布时间】:2015-11-15 15:22:45
【问题描述】:

插入并编辑注册表,但没有日期列。 |

编程看似不错,但未能正确注册日期字段。

使用 UTF8 编码,语言环境为 (Es)

为什么? T_T

表格

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

型号

这是桌子的模型

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'];   }

控制器

这是桌子的控制器

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

【问题讨论】:

    标签: php mysql date laravel laravel-5


    【解决方案1】:

    将这些行移到fill之前:

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

    然后使用fill 和其余代码:

    $Product->fill($request->all());
    $Product->save();
    
    // refirect or whatever...
    

    顺便说一句,您可以使用单个 mergearray

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

    【讨论】:

    • 谢谢你,我太不知所措了,我没有看到
    猜你喜欢
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    相关资源
    最近更新 更多