카테고리 없음

Database\Eloquent\Collection::update does not exist / get() 여러 모델 인스턴스에 update

Sein_ 2023. 11. 24. 00:10
728x90

"Database\Eloquent\Collection::update does not exist" 에러는 일반적으로 Eloquent 컬렉션에 대해 update 메서드를 호출하려고 시도했을 때 발생합니다.

update 메서드는 Eloquent 모델에 대해 호출할 수 있는 것이지 컬렉션에 대해서는 호출할 수 없습니다.

 

            $billing = Bill::findOr($request->billing_no, function () {
                throw new \Exception('빌링을 찾을 수 없습니다');
            });
            
            if ($billing->grip) {
                $billing->grip->billing_no = null;
                $billing->grip->update();
            }

 

$billing 의 결과는 컬렉션이며, 이 컬렉션은 여러 개의 Eloquent 모델 인스턴스를 포함합니다.

그러나 update 메서드는 개별 모델 인스턴스에 대해서만 사용할 수 있습니다.

 

            if ($billing->grip) {
                $billing->grip->each(function ($grip){
                    $grip->billing_no = null;
                    $grip->update();
                });
            }

 

이렇게 하면 컬렉션 내의 각 모델에 대해 update 메서드가 호출되어 업데이트가 이루어집니다.