Relationships Fields

Atomic Panel ships with built in relationships fields ready to use

Fields

BelongsTo Field

The BelongsTo field corresponds to a belongsTo Eloquent relationship. For example:

use MustafaKhaled\AtomicPanel\Fields\BelongsTo;

BelongsTo::make('Display Text', 'column_id', 'AtomicModelName', AtomicModelName::class),
1
2
3

let's assume a Post model belongsTo a User model. We may add the relationship to our Post AtomicModel like so:

public function User()
{
    return $this->belongsTo('App\Models\User', 'user_id','member_id');
}  
1
2
3
4

then lets define our BelongsTo field like so:

BelongsTo::make('User', 'user_id', 'User', User::class),
1

if you have foreign key instead of id just like member_id then you have to define it in foreignKey method :

BelongsTo::make('User', 'user_id', 'User', User::class)->foreignKey('member_id'),
1

by default the display key for the relation model is id to change it , you may use displayKey method:

BelongsTo::make('User', 'user_id', 'User', User::class)->displayKey('name'),
1

HasMany Fields

The HasMany field corresponds to a hasMany Eloquent relationship. For example:

use MustafaKhaled\AtomicPanel\Fields\BelongsTo;

HasMany::make('Display', 'AtomicModelName', AtomicModelName::class),
1
2
3

let's assume a User model hasMany Post models. We may add the relationship to our User AtomicModel like so:

public function Posts()
{
    return $this->hasMany('App\Models\Post', 'user_id');
}  
1
2
3
4

then lets define our HasMany field like so:

HasMany::make('Posts', 'Post', Post::class),
1