https://laravel-news.com/eloquent-tips-tricks : 정리
- Increments and Decrements :
increment , decrement 로 값을 증감
- XorY Methods : if(find)-abort →
findOrFail , if(first)-create → firstOrCreate
- Model boot() Method : 기본 동작이나 특정 이벤트를 위한 커스텀 로직을 추가
- Relationship with Conditions and Ordering : (관계 정의 시) 조건이나 정렬을 추가
- Model Properties : 모델의 다양한 기본 속성들
- Find Multiple Entries : find([1,2,3])
- whereX Methods : 필드명을 메서드명에 추가해
where 을 간단히 사용
- Order by Relationship : 4번을 응용하여 with, load 사용 시 정렬 가능
- 컨트롤러에서
sortByDesc 를 사용할 수 있음
- Eloquent::when() : if-else 대신
when()
- BelongsTo Default Models : $post->author->name ?? '’ 대신
belongsTo 기본 모델을 설정
- Order by Mutator :
get()->sortBy()
- Default Ordering in Global Scope : 글로벌 스코프로 기본 정렬을 설정
- Raw Query Methods :
whereRaw, havingRaw, orderByRaw
- Replicate : 데이터(모델) 복제
- Chunk() Method for Big Tables : 큰 데이터셋을 처리할 때
chunk 사용
- Create Additional Things When Creating a Model : artisan make:model Company -mcr