Simplifying Belongs To Relationship Queries
A useful addition to the Laravel framework was added recently, which allows for a cleaner syntax when retrieving items via a 'belongsTo' relationship.
Previously if you wanted to retrieve posts for a specific user you would build out a where clause as follows:
use App\Models\Post; $posts = Post::where('user_id', $user->id)->get();
A newer and cleaner syntax allows you to do the following:
$posts = Post::whereBelongsTo($user)->get();
You may also manually specify which relationship to query by providing the name of the relationship as the second argument:
$posts = Post::whereBelongsTo($user, 'author')->get();
I'm always a huge fan of changes like these that improve code readability.