티스토리 뷰
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
- redis
- addMonthWithoutOverflow
- Laravel
- eloquent-observer
- graphql-php
- exception-test
- session+token authorize
- addMonth
- 테스트_다중트랜잭션
- php
- 라라벨
- l5-swagger
- l5-swagger-response
- MySQL
- django
- laravel-kafka
- aaa패턴
- 메일
- POP3
- password-manager
- observer 매개변수 전달하기
- graphql
- 정규식
- laravel-test
- bitwarden-cli
- Python
- php-laravel
- 정의
- 자바스크립트
- vim
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함