【问题标题】:Laravel: fields not insertingLaravel:字段未插入
【发布时间】:2016-08-27 23:08:10
【问题描述】:

我有四个字段没有插入到我的产品表中,我检查了这些字段是否有值但它们没有被插入,并且这些字段由一个选择框填充,created_by 字段除外。

产品表架构:

Schema::create('products', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('supplier_id')->unsigned();
    $table->integer('category_id')->unsigned();
    $table->integer('brand_id')->unsigned();
    $table->string('title');
    $table->string('slug'); 
    etc....
}); 

保存产品:

$product = Product::create([
    'created_by'     => Auth::user()->id,
    'supplier_id'    => intval($request['supplier_id']),
    'category_id'    => $request['category_id'],
    'brand_id'       => $request->get('brand_id'),
    'title'          => $request->get('title'),
    'slug'           => str_slug( $request->get('title'), '-' ),
    etc...
)};

所有字段在提交时都包含值,但字段(supplier_idcategory_idbrand_idcreated_by)没有被插入到表中 - 它们的值是字符串

我的模特:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = "products";

    protected $fillable = [
        'supplier_id', 'category_id', 'brand_id', 'title', 'slug',
        'created_by', 'updated_by'
    ];
}


use Illuminate\Database\Migrations\Migration;

class CreateTableProducts extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Create table for storing products
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('supplier_id')->unsigned();
            $table->integer('category_id')->unsigned();
            $table->integer('brand_id')->unsigned();
            $table->string('title');
            $table->string('slug');            
            $table->string('keywords', 160);
            $table->integer('template_id')->unsigned();            
            $table->text('video_clip')->nullable();
            $table->text('body')->nullable();            
            $table->boolean('active');
            $table->boolean('clearance');
            $table->string('sku', 100);
            $table->string('quantity');
            $table->decimal('unit_price');    
            $table->decimal('sell_price');
            $table->decimal('sale_discount')->nullable();
            $table->datetime('sale_start')->nullable();
            $table->datetime('sale_end')->nullable();
            $table->string('promo_code', 20)->nullable();
            $table->double('promo_discount')->nullable();            
            $table->datetime('promo_start')->nullable();
            $table->datetime('promo_end')->nullable();
            $table->decimal('default_freight');
            $table->decimal('international_freight')->nullable();
            $table->decimal('expedited_freight')->nullable();
            $table->string('weight');
            $table->string('dimensions');
            $table->text('measurements')->nullable();
            $table->integer('created_by')->unsigned();
            $table->integer('updated_by')->unsigned()->nullable();            
            $table->timestamps();
            $table->unique(array('title','slug'));
        });

        Schema::table('products', function(Blueprint $table) {            
            $table->foreign('created_by')->references('id')->on('users');
            $table->foreign('updated_by')->references('id')->on('users');
            $table->foreign('supplier_id')->references('id')->on('suppliers');
            $table->foreign('category_id')->references('id')->on('categories');
            $table->foreign('brand_id')->references('id')->on('brands');
            $table->foreign('template_id')->references('id')->on('templates');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('products');
    }
}

【问题讨论】:

  • 你检查你的表格了吗?只有四个字段,那么其他字段呢?
  • 这四个字段不会插入,但其他所有字段都会插入
  • 请创建一个新变量(即 $data)并将它分配给您传递给 Product::create() 的数组。然后执行var_dump($data) 并确保填写所有必填字段。
  • 在你的保存方法检查检查$input = $request-&gt;all();dd($input);天气你会从你的表格中得到输入??
  • protected $fillable 的模型中仅使用protected $guarded = [];

标签: php laravel


【解决方案1】:

在“保存产品”位中,要实际保存产品,您必须调用: $product->save();

编辑-确实你不需要。不知道我是应该删除它还是把它留在这里。

【讨论】:

  • 使用Model::create([])时无需调用save()
  • 好吧,我的错。那你有什么错误吗?如果没有插入,应该有一个。
  • 并不总是抛出错误,他成功地将行插入到表中,他只是缺少字段。
猜你喜欢
  • 1970-01-01
  • 2020-05-02
  • 2019-01-14
  • 2018-08-05
  • 1970-01-01
  • 1970-01-01
  • 2018-12-20
  • 2021-09-07
  • 1970-01-01
相关资源
最近更新 更多