Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

php - Eloquent SQL to group by relation with pivot table

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...