TIL Laravel 12/6/2023

엽토군·2023년 12월 6일
0

TIL

목록 보기
12/13

Eloquent: 오직 더러운 속성만이 갱신받는다

다음 코드를 보자.

$foo = Foo::find(7787); // Foo 모델은 자동 updated_at이 있는 모델이라고 가정한다.
$foo->bar = 1;
$foo->save();
$foo->bar = 1;
$foo->save();
$foo->bar = 1;
$foo->save();

이 코드가 실행되는 동안 실제로 커밋되는 쿼리들은, 다음과 같지 않다 :

update foo set bar = 1, updated_at = now() where id = 7787;
update foo set bar = 1, updated_at = now() where id = 7787;
update foo set bar = 1, updated_at = now() where id = 7787;

실제로 커밋되는 것은 다음과 같다.

update foo set bar = 1, updated_at = now() where id = 7787;
update foo set          updated_at = now() where id = 7787;
update foo set          updated_at = now() where id = 7787;

Eloquent는 original과 다른 (즉, "dirty"한) attribute만 골라서 update 쿼리 빌딩에 사용한다. 즉, dirty한 attribute만이 update를 받는다.

정 뭐하면 forceUpdate()를 하면 되겠지만... 그럴 필요가 어디 있겠는가? 로그 읽다가 "어?" 하지나 않으면 다행이라 할 것이다.

profile
4년차 PHP 개발자입니다.

0개의 댓글