Skip to content

Relationships

The AutoCrud trait provides comprehensive support for Eloquent relationships, including belongsTo, morphTo, and belongsToMany 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],
]
PropertyTypeRequiredDescription
modelstringYesFull class path to the related model
relationstringYesMethod name (resolved dynamically via __call)
tableKeystringNoTemplate for displaying in tables
formKeystringNoTemplate for displaying in forms
polymorphicboolNoWhether this is a morphTo relationship
morphTypestringNoType column name (required if polymorphic = true)
serverSideboolNoWhether formKey/tableKey is resolved on backend
storeShortcutboolNoUseful for pivot tables - allows quick creation

Use curly braces to reference related model attributes:

'tableKey' => '{name} - {email}',
'formKey' => '{name}',

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,
]

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})',
],
];
PropertyTypeRequiredDescription
relationstringYesRelationship method name
namestringYesDisplay name for the relationship
modelstringYesRelated model class
pivotTablestringYesPivot table name
foreignKeystringYesForeign key in pivot table
relatedKeystringYesRelated key in pivot table
pivotModelstringNoPivot model class
pivotFieldsarrayNoFields for the pivot table
tableboolNoShow in tables
formKeystringNoDisplay template

The trait automatically:

  1. Adds endpoints to each relation and pivot fields
  2. Uses withPivot() with all pivot columns if pivotFields are defined
  3. Applies withTrashed() if the related model uses SoftDeletes
  4. Resolves relationships dynamically via the __call method

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

The trait uses __call() to dynamically resolve relationships:

// This automatically creates the relationship method
public function user()
{
return $this->belongsTo(User::class);
}
// For external relations
public function vehicles()
{
return $this->belongsToMany(Vehicle::class, 'reservations_vehicles')
->withPivot(['quantity'])
->withTrashed();
}

The trait automatically detects and handles soft deletes:

// If User model uses SoftDeletes, this is automatically applied
public function user()
{
return $this->belongsTo(User::class)->withTrashed();
}
<?php
namespace 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})',
],
];
}