Laravel 5.4忽略$ fillable

时间:2017-09-08 09:09:52

标签: php mysql laravel eloquent

我正在尝试更新订单表,该表具有由 customer_id 链接到客户表的freign键约束。

迁移文件(100%有效):

Schema::create('orders', function (Blueprint $table) {
            $table->integer('order_id')->unsigned()->index();
            $table->integer('customer_id')->unsigned()->index();
            $table->foreign('customer_id')->references('customer_id')->on('customers')->onDelete('cascade');
            $table->string('order_status');
            $table->string('customer_note')->nullable();
            $table->timestamp('order_date');
            $table->timestamps();
            $table->softDeletes();
            $table->primary(['order_id', 'customer_id']);
        }); 

在我的模型中,我将以下列填充,以便Laravel不会忽略它们:

protected $fillable = [
        'order_id', 'customer_id', 'order_status', 'customer_note', 'order_date',
    ];

当我创建/更新订单时,我在update方法下的OrderController中使用以下代码行。我使用 firstOrCreate()来确保如果订单被删除或从未添加到第一位,这不会失败。

$order = Order::firstOrCreate(['order_id' => $request->input('id')]);
        $order->order_id = $request->input('id');
        $order->customer_id = $request->input('customer_id');
        $order->order_status = $request->input('status');
        $order->customer_note = $request->input('customer_note');
        $order->order_date = $request->input('date_created');
        $order->save();

当我尝试更新订单时,我在日志文件中收到以下错误消息:

Next Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`vcc-backoffice`.`orders`, CONSTRAINT `orders_customer_id_foreign` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`) ON DELETE CASCADE) (SQL: insert into `orders` (`order_id`, `updated_at`, `created_at`) values (76, 2017-09-08 08:55:37, 2017-09-08 08:55:37)) in /home/vagrant/Projects/vcc-backoffice/vendor/laravel/framework/src/Illuminate/Database/Connection.php:647 

我注意到insert语句只尝试插入3列。 order_idupdated_atcreated_at

我假设无法创建行,因为外部* customer_id ** colun没有被填充,但我无法弄清楚为什么Laravel会忽略它们。

  1. 我想也许,输入的格式不正确,甚至硬编码customer_id,因为1不起作用。 (customer_id 1存在于customers表中)。

  2. 我还检查并确认 $ request->输入('customer_id')包含正确的整数,在这种情况下为1并且作为记录存在客户表。

  3. 我怀疑Laravel忽略了相关的专栏,但我无法弄清楚原因。

    我的模型文件如下所示:

    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Database\Eloquent\SoftDeletes;
    
    class Order extends Model
    {
        use Notifiable;
        use SoftDeletes;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'order_id', 'customer_id', 'order_status', 'customer_note', 'order_date',
        ];
    
        /**
         * Whether the primary key auto-increments.
         *
         * @var bool
         */
        public $incrementing = false;
    
        /**
         * Set the primary key.
         *
         * @var string
         */
        protected $primaryKey = ['order_id', 'customer_id'];
    
        protected $with = ['products']; 
    
        /**
         * The products that belong to the Order.
         */
        public function products()
        {
            return $this->belongsToMany('App\Product','order_product','order_id','product_id')
                ->withPivot('qty')
                ->withTimeStamps();
        }
    
        /**
         * The customer that belongs to the Order.
         */
        public function customer()
        {
            return $this->belongsTo('App\Customer');
        }
    }
    

1 个答案:

答案 0 :(得分:0)

firstOrCreate如果无法根据您提供的属性获取行,则会创建一个新的数据库条目,但是不允许创建一个没有customer_id的新行,因为您添加了一个外来的列上的键,不能是null

null添加到您的列

$table->integer('customer_id')->unsigned()->index()->nullable();

或将firstOrCreate更改为:

$order = Order::where('order_id', $request->input('id'))->first() ?: new Order;