with relation laravel

94

with relation laravel -

//Single relationship
$billings = Billing::with('user')->get();
// sinlge with selected colums
$billings = Billing::with('user:id,name')->get();

// multiple relations
$billings = Biling::with(['user', 'subscription'])->get();

// nested relations
$schools = School::with('class.user')->get();

// Constraining Eager Loads
$users = User::with(['posts' => function ($query) {
    $query->where('title', 'like', '%code%');
}])->get();
$users = User::with(['posts' => function ($query) {
    $query->orderBy('created_at', 'desc');
}])->get();

eloquent relationships -

$roles = App\User::find(1)->roles()->orderBy('name')->get();

Comments

Submit
0 Comments