php - Laravel 5.4 忽略 $fillable

标签 php mysql laravel eloquent

我正在尝试更新一个 orders 表,该表具有通过 customer_id 链接到客户表的外键约束。

迁移文件(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',
    ];

当我创建/更新订单时,我在 OrderController 的 update 方法下使用以下代码行。我使用 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 

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

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

  1. 我想也许输入的格式不正确,但即使将 customer_id 硬编码为 1 也不起作用。 (customer_id 1 存在于客户表中)。

  2. 我还检查并确认了 $request->input('customer_id') 包含正确的整数,在本例中为 1,并且确实作为记录存在于顾客表。

我怀疑 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');
    }
}

最佳答案

如果

firstOrCreate 无法根据您提供的属性获取行,它将创建一个新的数据库条目,但不允许创建没有 customer_id 的新行,因为您已在该列上添加了外键,并且它不能为 null

null 添加到您的列中,

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

或将 firstOrCreate 更改为:

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

关于php - Laravel 5.4 忽略 $fillable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46112759/

相关文章:

javascript - 使用样式控制弹出窗口

javascript - jquery 删除所有 div,即使它们位于另一个菜单中

PHP更改数组键

mysql - sqlpro重复输入错误

php - 具有两个外键的三个表之间的 Laravel 关系

mysql - MySQL 中用于分隔字段的多行文本字段

mysql - 如果两列具有特定值,如何设置值?

php - 如何使用 laradock 使用 postgres 配置 laravel?无法连接到服务器 : Connection refused on port 5432

macos - 如何在 mac os x yosemite (10.10) 上安装 mcrypt 扩展

php - Laravel 在事务中插入父子失败