我有四個字段沒有插入到我的產品表中,我檢查過這些字段是否有值,但它們沒有被插入,並且這些字段由選擇框填充,但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_id
,category_id
,brand_id
,created_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');
}
}
你檢查了你的表格嗎?那麼只有四個提起那麼其他領域呢? – Hamelraj
這四個字段不會插入,但其他所有內容都會插入 – ONYX
請創建一個新變量(即$ data),並將它傳遞給Product :: create()的數組。然後執行'var_dump($ data)'並確保所有必填字段都已填充。 – starky