2016-05-03 57 views
1

我有四個字段沒有插入到我的產品表中,我檢查過這些字段是否有值,但它們沒有被插入,並且這些字段由選擇框填充,但created_by字段除外。Laravel:字段沒有插入

產品表模式:

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

保存產品:提交但字段(supplier_idcategory_idbrand_idcreated_by)不被插入時

$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... 
)}; 

所有字段保存值表 - 它們的值是字符串

我的模型:

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

你檢查了你的表格嗎?那麼只有四個提起那麼其他領域呢? – Hamelraj

+0

這四個字段不會插入,但其他所有內容都會插入 – ONYX

+0

請創建一個新變量(即$ data),並將它傳遞給Product :: create()的數組。然後執行'var_dump($ data)'並確保所有必填字段都已填充。 – starky

回答

-1

在「節能產品」位,實際節省您必須調用產品: $產品 - >保存();

編輯 - 事實上你不需要。不知道我應該刪除這個還是把它留在這裏。

+0

當使用'Model :: create([])'時,你不需要調用'save()' –

+0

對,我的不好。那你是否會遇到某種錯誤?如果沒有插入,應該有一個。 – overburn

+0

一個錯誤並不總是被拋出,他成功地將該行插入到表中,他只是缺少字段。 –