【发布时间】: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_id、category_id、brand_id、created_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->all();dd($input);天气你会从你的表格中得到输入?? -
在
protected $fillable的模型中仅使用protected $guarded = [];