티스토리 뷰
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 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 라라벨
- laravel-test
- 정규식
- php-laravel
- php
- laravel-kafka
- graphql
- Python
- 테스트_다중트랜잭션
- observer 매개변수 전달하기
- redis
- eloquent-observer
- vim
- exception-test
- django
- graphql-php
- addMonth
- Laravel
- l5-swagger
- 메일
- MySQL
- 정의
- 자바스크립트
- addMonthWithoutOverflow
- password-manager
- session+token authorize
- aaa패턴
- l5-swagger-response
- bitwarden-cli
- POP3
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함