Relationships
Relationships
Section titled “Relationships”The AutoCrud trait provides comprehensive support for Eloquent relationships, including belongsTo, morphTo, and belongsToMany relationships.
BelongsTo Relationships
Section titled “BelongsTo Relationships”Define belongsTo relationships using the relation property in field definitions:
[ 'name' => 'User', 'field' => 'user_id', 'type' => 'number', 'relation' => [ 'model' => User::class, 'relation' => 'user', 'tableKey' => '{name}', 'formKey' => '{name}', ], 'table' => true, 'form' => true, 'rules' => ['required' => true],]Relation Properties
Section titled “Relation Properties”| Property | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Full class path to the related model |
relation | string | Yes | Method name (resolved dynamically via __call) |
tableKey | string | No | Template for displaying in tables |
formKey | string | No | Template for displaying in forms |
polymorphic | bool | No | Whether this is a morphTo relationship |
morphType | string | No | Type column name (required if polymorphic = true) |
serverSide | bool | No | Whether formKey/tableKey is resolved on backend |
storeShortcut | bool | No | Useful for pivot tables - allows quick creation |
Template Keys
Section titled “Template Keys”Use curly braces to reference related model attributes:
'tableKey' => '{name} - {email}','formKey' => '{name}',MorphTo Relationships
Section titled “MorphTo Relationships”For polymorphic relationships, set polymorphic to true and specify the morphType column:
[ 'name' => 'Commentable', 'field' => 'commentable_id', 'type' => 'number', 'relation' => [ 'model' => null, // Not needed for morphTo 'relation' => 'commentable', 'polymorphic' => true, 'morphType' => 'commentable_type', 'tableKey' => '{name}', 'formKey' => '{name}', ], 'table' => true, 'form' => true,]External Relations (BelongsToMany)
Section titled “External Relations (BelongsToMany)”For complex many-to-many relationships, use the $externalRelations property:
protected static $externalRelations = [ [ 'relation' => 'vehicles', 'name' => 'Vehículos', 'model' => Vehicle::class, 'pivotTable' => 'reservations_vehicles', 'foreignKey' => 'reservation_id', 'relatedKey' => 'vehicle_id', 'pivotModel' => ReservationVehicle::class, // optional 'pivotFields' => [ [ 'name' => 'Quantity', 'field' => 'quantity', 'type' => 'number', 'form' => true, 'rules' => ['required' => true], ], ], 'table' => false, 'formKey' => '{vehicletype.name} ({code})', ],];External Relation Properties
Section titled “External Relation Properties”| Property | Type | Required | Description |
|---|---|---|---|
relation | string | Yes | Relationship method name |
name | string | Yes | Display name for the relationship |
model | string | Yes | Related model class |
pivotTable | string | Yes | Pivot table name |
foreignKey | string | Yes | Foreign key in pivot table |
relatedKey | string | Yes | Related key in pivot table |
pivotModel | string | No | Pivot model class |
pivotFields | array | No | Fields for the pivot table |
table | bool | No | Show in tables |
formKey | string | No | Display template |
Automatic Relationship Handling
Section titled “Automatic Relationship Handling”The trait automatically:
- Adds endpoints to each relation and pivot fields
- Uses
withPivot()with all pivot columns ifpivotFieldsare defined - Applies
withTrashed()if the related model uses SoftDeletes - Resolves relationships dynamically via the
__callmethod
Includes Configuration
Section titled “Includes Configuration”Control which relationships are always loaded using the $includes property:
protected static $includes = [ 'user', 'category', 'tags',];The getIncludes() method automatically adds:
'records.user'- Relationships defined in
getFields() - Relationships from
$externalRelations
Dynamic Relationship Resolution
Section titled “Dynamic Relationship Resolution”The trait uses __call() to dynamically resolve relationships:
// This automatically creates the relationship methodpublic function user(){ return $this->belongsTo(User::class);}
// For external relationspublic function vehicles(){ return $this->belongsToMany(Vehicle::class, 'reservations_vehicles') ->withPivot(['quantity']) ->withTrashed();}Soft Deletes Support
Section titled “Soft Deletes Support”The trait automatically detects and handles soft deletes:
// If User model uses SoftDeletes, this is automatically appliedpublic function user(){ return $this->belongsTo(User::class)->withTrashed();}Complete Example
Section titled “Complete Example”<?phpnamespace App\Models;
use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\SoftDeletes;use Ismaelcmajada\LaravelAutoCrud\Models\Traits\AutoCrud;
class Reservation extends Model{ use AutoCrud, SoftDeletes;
protected static function getFields(): array { return [ [ 'name' => 'User', 'field' => 'user_id', 'type' => 'number', 'relation' => [ 'model' => User::class, 'relation' => 'user', 'tableKey' => '{name} ({email})', 'formKey' => '{name}', ], 'table' => true, 'form' => true, 'rules' => ['required' => true], ], [ 'name' => 'Start Date', 'field' => 'start_date', 'type' => 'datetime', 'table' => true, 'form' => true, 'rules' => ['required' => true], ], [ 'name' => 'End Date', 'field' => 'end_date', 'type' => 'datetime', 'table' => true, 'form' => true, 'rules' => ['required' => true], ], ]; }
protected static $includes = [ 'user', ];
protected static $externalRelations = [ [ 'relation' => 'vehicles', 'name' => 'Vehicles', 'model' => Vehicle::class, 'pivotTable' => 'reservations_vehicles', 'foreignKey' => 'reservation_id', 'relatedKey' => 'vehicle_id', 'pivotModel' => ReservationVehicle::class, 'pivotFields' => [ [ 'name' => 'Quantity', 'field' => 'quantity', 'type' => 'number', 'form' => true, 'rules' => ['required' => true], ], ], 'formKey' => '{name} ({code})', ], ];}