freshbitsweb/laratables

Failed to Acces Relation

zendzo opened this issue · 3 comments

i have 3 relations

user ->hasOne->room
room->belongsTo->floor
floor->hasMany->room

{ name: 'id' },
{ name: 'name' },
{ name: 'email' },
{ name: 'action', orderable: false, searchable: false },
{ name: 'room.name', orderable: false },
{ name: "room.floor.level" } // this show [object Object] on the table

i get the value of floor level by defining custom column
but it is possible if i want to access floor level through room.floor.level i think this is simpler

public static function laratablesCustomLevel($user)
    {
        return $user->room->floor->level;
    }
columns: [
                  { name: 'id' },
                  { name: 'name' },
                  { name: 'email' },
                  { name: 'action', orderable: false, searchable: false },
                  { name: 'room.name', orderable: false },
                  { name: 'level' }
              ]

You're right @zendzo

Nested relationships aren't supported yet.

You have used the custom column the right way but you should also tell the query to eager load data to prevent n+1 issue.

/**
 * Specify additional conditions for the query, if any.
 *
 * @param \Illuminate\Database\Eloquent\Builder
 * @param \Illuminate\Database\Eloquent\Builder
 */
public static function laratablesQueryConditions($query)
{
    return $query->with([ 'room.floor.level' ]);
}

As this isn't a common use case, I would prefer not to add support for now. Have already added the note in the readme.