1<?php
2//For example, imagine a blog application in which a User model has many associated Post models:
3namespace App\Models;
4use Illuminate\Database\Eloquent\Model;
5class User extends Model
6{
7 /**
8 * Get all of the posts for the user.
9 */
10 public function posts()
11 {
12 return $this->hasMany(Post::class);
13 }
14}
15
16//You may query the posts relationship and add additional constraints to the relationship like so:
17use App\Models\User;
18$user = User::find(1);
19$user->posts()->where('active', 1)->get();
20