I have two Models, a Track
model and an Artist
model, where each Track
model belongsTo()
one Artist
model. Track
models have a number
column and Artist
models have a name
column. I want to retrieve all Tracks
where number = 1
and then display those sorted by their Artist's name. So far I have only figured out how to do one or the other, but not both.
I would expect to be able to do this with something like:
Track::with(['artist' => function($q) {
$q->orderBy('name');
}])->where('number', 1)->get();
This displays all tracks where number = 1
, but the tracks
are not sorted in any particular order. I get the same result if I remove the with()
altogether. Conversely, if I remove the where()
clause then I get all Tracks in the database, and they are sorted by the Artist's name. How can I combine these two functions? The results should look like:
[
{
"id": 17,
"created_at": "2019-09-04 22:33:50",
"updated_at": "2019-09-04 22:33:50",
"number": 1,
"title": "Iste omnis maxime inventore rerum nam et.",
"artist_id": 2,
"album_id": 4,
"length": "06:08",
"disc_number": 1,
"artist": {
"id": 2,
"created_at": "2019-09-04 22:33:46",
"updated_at": "2019-09-04 22:33:49",
"name": "Ace",
"image_id": "4"
}
},
{
"id": 35,
"created_at": "2019-09-04 22:33:54",
"updated_at": "2019-09-04 22:33:54",
"number": 1,
"title": "Doloremque quidem voluptatibus doloribus et.",
"artist_id": 4,
"album_id": 7,
"length": "18:13",
"disc_number": 3,
"artist": {
"id": 4,
"created_at": "2019-09-04 22:33:46",
"updated_at": "2019-09-04 22:33:53",
"name": "Bar",
"image_id": "10"
}
},
{
"id": 54,
"created_at": "2019-09-04 23:00:08",
"updated_at": "2019-09-04 23:00:08",
"number": 1,
"title": "Ut placeat assumenda aut.",
"artist_id": 21,
"album_id": 17,
"length": "09:25",
"disc_number": 4,
"artist": {
"id": 21,
"created_at": "2019-09-04 23:00:08",
"updated_at": "2019-09-04 23:00:08",
"name": "Cat",
"image_id": "22"
}
},
{
"id": 71,
"created_at": "2019-09-04 23:00:11",
"updated_at": "2019-09-04 23:00:11",
"number": 1,
"title": "Omnis et dolores odio a eius.",
"artist_id": 22,
"album_id": 20,
"length": "16:48",
"disc_number": 2,
"artist": {
"id": 22,
"created_at": "2019-09-04 23:00:08",
"updated_at": "2019-09-04 23:00:10",
"name": "Dog",
"image_id": "25"
}
},
]