我正在try 更新有两个主键的模型.

型号

namespace App;

use Illuminate\Database\Eloquent\型号;

class Inventory extends 型号
{
    /**
     * The table associated with the model.
     */
    protected $table = 'inventories';

    /**
     * Indicates model primary keys.
     */
    protected $primaryKey = ['user_id', 'stock_id'];
...

Migration

Schema::create('inventories', function (Blueprint $table) {
    $table->integer('user_id')->unsigned();
    $table->integer('stock_id')->unsigned();
    $table->bigInteger('quantity');

    $table->primary(['user_id', 'stock_id']);

    $table->foreign('user_id')->references('id')->on('users')
        ->onUpdate('restrict')
        ->onDelete('cascade');
    $table->foreign('stock_id')->references('id')->on('stocks')
        ->onUpdate('restrict')
        ->onDelete('cascade');
});

This is code which should update Inventory model, but it doesn't.

$inventory = Inventory::where('user_id', $user->id)->where('stock_id', $order->stock->id)->first();
$inventory->quantity += $order->quantity;
$inventory->save();

I get this error:

Illegal offset type

I also tried to use updateOrCreate() method. It doesn't work (I get same error).

Can anyone tell how 型号 with two primary key should be updated?

推荐答案

我已经遇到这个问题好几次了.您需要覆盖一些属性:

protected $primaryKey = ['user_id', 'stock_id'];
public $incrementing = false;

and methods (credit):

/**
 * Set the keys for a save update query.
 *
 * @param  \Illuminate\Database\Eloquent\Builder  $query
 * @return \Illuminate\Database\Eloquent\Builder
 */
protected function setKeysForSaveQuery(Builder $query)
{
    $keys = $this->getKeyName();
    if(!is_array($keys)){
        return parent::setKeysForSaveQuery($query);
    }

    foreach($keys as $keyName){
        $query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
    }

    return $query;
}

/**
 * Get the primary key value for a save query.
 *
 * @param mixed $keyName
 * @return mixed
 */
protected function getKeyForSaveQuery($keyName = null)
{
    if(is_null($keyName)){
        $keyName = $this->getKeyName();
    }

    if (isset($this->original[$keyName])) {
        return $this->original[$keyName];
    }

    return $this->getAttribute($keyName);
}

Remember this code needs to reference Eloquent Builder class with

use Illuminate\Database\Eloquent\Builder;

I suggest putting those methods in a HasCompositePrimaryKey Trait so you can just use it in any of your models that need it.

Laravel相关问答推荐

子目录中具有Vue实例的Laravel不起作用

运行NPM Prod时出现VUE问题

在 React 中将属性从一个组件传递到另一个组件

Laravel:BelongstoMany 关系实现

Laravel 5.2 无法打开 laravel.log

Laravel Blade - 产生内部部分

Eloquent 的集合方法,例如 only 或 except 返回一个空集合

静态密码在默认的 Laravel 用户工厂中是如何工作的?

如何在 Eloquent 上设置条件关系

调用未定义的函数 mb_strimwidth

当表中不存在列时如何将 paginate() 与 having() 子句一起使用

Laravel 中的填充方法不起作用?

laravel 4:更新行时如何从列的当前值中减一?

控制器外部的 Laravel 访问请求对象

将自定义消息(或任何其他数据)传递给 Laravel 404.blade.php

Laravel php artisan 服务于模仿 HTTPS

你如何在 Laravel 中的每个响应上强制一个 JSON 响应?

Laravel 5.4 Vue.JS 无法挂载组件:未定义模板或渲染函数

为什么 phpunit 没有得到 phpunit.xml 中指定的正确 APP_ENV?

为什么`catch (Exception $e)` 不处理这个`ErrorException`?