Reputation: 1046
I want my publications relationship ordered by the 'ordering' column.
This is what I've come up with so far:
class Book extends Model
{
public $hasMany = [
'publications' => ['Codalia\Bookend\Models\Publication']
];
...
}
class Publication extends Model
{
public $belongsTo = [
'book' => ['Codalia\Bookend\Models\Book']
];
...
}
$book = Book::with(['publications' => function ($q){
$q->orderBy('ordering');
}])->where('id', $id)->get();
$book->publications;
but I get an error:
Property [publications] does not exist on this collection instance.
Why do I retrieve a collection and not a Book object ?
Upvotes: 0
Views: 27
Reputation: 8947
->get();
is gonna return a collection of many. What you are looking for is ->first();
$book = Book::with(['publications' => function ($q){
$q->orderBy('ordering');
}])->where('id', $id)->first(); // here
$book->publications;
Upvotes: 1