티스토리 뷰

Laravel

[laravel8] $model->unsetRelation()

rediate.will 2022. 11. 1. 14:56

이거로딩으로 먼저 로딩해서 그 데이터로 쓰고, 그대로 해당부분 릴레이션은 끊고 프론트로 값을 리턴하고 싶었다!

 

예를들면, A테이블에 연결된 B테이블의 내용을 가져와서 요리조리 사용하고, B테이블의 내용은 삭제하고 프론트로 주고 싶었다.

 

릴레이션 삭제하는게 분명 있을거 같아서 검색해서 결국 찾아내었어!

 

if ($user->isAdminUser()) {
	$order->load(['salon:id', 'salon.sales' => function ($query) {
    	$start_date = \Carbon\Carbon::now()->subWeek()->format('Y-m-d');
    	$end_date = Carbon::now()->format('Y-m-d');
    	$query->whereBetween('date', [$start_date, $end_date]);
    }]);

    $order->daily_sale = $order->salon->sales->where('date', Carbon::today())->sum('use_count');
    $order->weekly_sale = $order->salon->sales->sum('use_count');

    $order->salon->unsetRelation('sales');
}

 

unsetRelation이 있었다!

 

https://github.com/laravel/framework/blob/8.x/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php#L809

 

GitHub - laravel/framework: The Laravel Framework.

The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.

github.com

 

요기서 확인 할수 이쏘요!

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함