1 개요[ | ]
- 라라벨 쿼리빌더 upsert
- 2018년 1월 현재, upsert 메소드는 제공되지 않음
- PK 컬럼이 1개 있을 때 아래와 같이 구현 가능
하드코딩
PHP
Copy
foreach( $rows as $row ) {
$where = DB::table('table1')->where('id', $row['id']);
if( $where->first() ) $where->update($row);
else DB::table('table1')->insert($row);
}
테이블명, 주키명 변수화
PHP
Copy
$table = 'table1';
$pk = 'id';
foreach( $rows as $row ) {
$where = DB::table($table)->where($pk, $row[$pk]);
if( $where->first() ) $where->update($row);
else DB::table($table)->insert($row);
}
2 같이 보기[ | ]
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.