Eloquent makes it easy to manage relationships between database tables. Lyger supports several types of relationships including One-to-One, One-to-Many, and Many-to-Many, allowing you to work with related data in an expressive and intuitive way.
// Get all posts for a user$user = User::find(1);$posts = $user->posts()->get();foreach ($posts as $post) { echo $post->title; echo $post->content;}// Get first post$firstPost = $user->posts()->first();// Count posts$postCount = $user->posts()->count();// Get post's author$post = Post::find(1);$author = $post->user()->get();echo $author->name;
The pivot table name is automatically determined by combining the related model names in alphabetical order: post_tag. You can customize this by passing a second parameter to belongsToMany().
// Get all tags for a post$post = Post::find(1);$tags = $post->tags()->get();foreach ($tags as $tag) { echo $tag->name;}// Get all posts with a specific tag$tag = Tag::find(1);$posts = $tag->posts()->get();foreach ($posts as $post) { echo $post->title;}
The foreign key on the related model (defaults to {model}_id)
Copy
public function profile(){ return $this->hasOne(Profile::class);}// With custom foreign keypublic function profile(){ return $this->hasOne(Profile::class, 'custom_user_id');}
The foreign key on the related model (defaults to {model}_id)
Copy
public function posts(){ return $this->hasMany(Post::class);}// With custom foreign keypublic function posts(){ return $this->hasMany(Post::class, 'author_id');}
The foreign key on this model (defaults to {related_model}_id)
Copy
public function user(){ return $this->belongsTo(User::class);}// With custom foreign keypublic function author(){ return $this->belongsTo(User::class, 'author_id');}
The name of the pivot table (defaults to alphabetically ordered model names)
Copy
public function tags(){ return $this->belongsToMany(Tag::class);}// With custom pivot tablepublic function tags(){ return $this->belongsToMany(Tag::class, 'article_tags');}
Here’s a comprehensive example with multiple relationship types:
app/Models/User.php
Copy
<?phpnamespace App\Models;use Lyger\Database\Model;class User extends Model{ protected string $table = 'users'; protected array $fillable = [ 'name', 'email', 'password' ]; protected array $hidden = ['password']; // One-to-One: User has one Profile public function profile() { return $this->hasOne(Profile::class); } // One-to-Many: User has many Posts public function posts() { return $this->hasMany(Post::class); } // One-to-Many: User has many Comments public function comments() { return $this->hasMany(Comment::class); }}
app/Models/Post.php
Copy
<?phpnamespace App\Models;use Lyger\Database\Model;class Post extends Model{ protected string $table = 'posts'; protected array $fillable = [ 'user_id', 'title', 'content', 'status' ]; protected array $casts = [ 'published' => 'bool' ]; // Many-to-One: Post belongs to User public function user() { return $this->belongsTo(User::class); } // One-to-Many: Post has many Comments public function comments() { return $this->hasMany(Comment::class); } // Many-to-Many: Post has many Tags public function tags() { return $this->belongsToMany(Tag::class); }}
app/Models/Comment.php
Copy
<?phpnamespace App\Models;use Lyger\Database\Model;class Comment extends Model{ protected string $table = 'comments'; protected array $fillable = [ 'user_id', 'post_id', 'content' ]; // Many-to-One: Comment belongs to User public function user() { return $this->belongsTo(User::class); } // Many-to-One: Comment belongs to Post public function post() { return $this->belongsTo(Post::class); }}
app/Models/Tag.php
Copy
<?phpnamespace App\Models;use Lyger\Database\Model;class Tag extends Model{ protected string $table = 'tags'; protected array $fillable = ['name', 'slug']; // Many-to-Many: Tag has many Posts public function posts() { return $this->belongsToMany(Post::class); }}
// Get user with all relationships$user = User::find(1);// Get user's profile$profile = $user->profile()->get();echo $profile->bio;// Get user's posts$posts = $user->posts()->get();foreach ($posts as $post) { echo $post->title; // Get post's tags $tags = $post->tags()->get(); foreach ($tags as $tag) { echo $tag->name; } // Get post's comments $comments = $post->comments()->get(); foreach ($comments as $comment) { echo $comment->content; // Get comment author $author = $comment->user()->get(); echo $author->name; }}// Get user's comments$comments = $user->comments()->get();echo "Total comments: " . $comments->count();// Get all posts with a specific tag$tag = Tag::find(1);$taggedPosts = $tag->posts()->get();
// Create pivot table for posts and tagspublic function up(): void{ $this->getSchema()->create('post_tag', function ($table) { $table->id(); $table->integer('post_id')->unsigned(); $table->integer('tag_id')->unsigned(); $table->timestamps(); // Composite index for unique pairs $table->index(['post_id', 'tag_id']); });}