티스토리 뷰

Laravel9 모델 PrimayKey가 2개 이상일 때 create, update 사용 시

Illegal offset type

//model.php
<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

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

    /**
     * Indicates model primary keys.
     */
    protected $primaryKey = ['user_id', 'stock_id'];
    ...
//controller.php
$inventory = Inventory::where('user_id', $user->id)
    ->where('stock_id', $order->stock->id)->first();
$inventory->quantity += $order->quantity;
$inventory->save();

//Illegal offset type

  • 해결방법
    setKeysForSaveQuery getKeyForSaveQuery overridden function
/**
 * 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);
}
  • make trait
$ php artisan make:trait HasCompositePrimaryKeyTrait
//HasCompositePrimaryKeyTrait.php
<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Builder;

trait HasCompositePrimaryKeyTrait
{
    /**
     * 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);
    }
}
//model.php
<?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Traits\HasCompositePrimaryKeyTrait;

class Inventory extends Model
{
    use HasCompositePrimaryKeyTrait;
...

'공부합시다 > php' 카테고리의 다른 글

라라벨 하나의 Router에서 여러개 인증 사용하기  (0) 2023.09.14
COMPOSER 2.x 버전 설치 및 업데이트  (0) 2023.07.13
PHP L5-Swagger 예시  (0) 2023.06.15
Codeigniter3 + graphql-php  (0) 2021.06.02
array_count_values  (0) 2021.03.10
댓글