My goal is to have a list of notes grouped by category so that I don't have doubles with the same category and I would like to get some help with writing the eloquent SQL. I have read Laravel/Eloquent documentation but I couldn't understand much. I have this DB structure:
Notes - which contains info about a specific note created by the user, like text
and note_date
.
Note_categories - which contains category_name
and color
.
Note_note_category - which contains note_id
and note_category_id
.
Note Model
public function categories()
{
return $this->belongsToMany(NoteCategory::class);
}
NoteCategory Model
public function notes()
{
return $this->belongsToMany(Note::class);
}
Controller
$notes = Note::with(['categories'])
->forParticipant($participant->id)
->whereMonth('note_date', $date->month)
->get();
question from:
https://stackoverflow.com/questions/66061948/eloquent-sql-to-group-by-relation-with-pivot-table 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…