Eloquent Relationship Generator Laravel 5.x+

  1. Home
  2. > Web Dev > Laravel Tools >
  3. Eloquent Relationship Generator
Generated PHP Code

    

How to Use

  1. Source Model — The model that defines the relationship (e.g., User).
  2. Target Model — The related model (e.g., Post).
  3. Relationship Type — Choose the relationship type that matches your database schema.
  4. Keys — Optional foreign key and local key overrides if you're not following Laravel's conventions.

Relationship Reference

  • hasOne — One-to-one (e.g., User has one Profile). Foreign key in target table.
  • hasMany — One-to-many (e.g., User has many Posts). Foreign key in target table.
  • belongsTo — Inverse one-to-one/many (e.g., Post belongs to User). Foreign key in source table.
  • belongsToMany — Many-to-many (e.g., User belongsToMany Role). Uses pivot table.
Help2Code Logo
Menu