InfyOmLabs / laravel-generator

API and Admin Panel CRUD Generator for Laravel.
https://www.infyom.com/open-source
MIT License
3.78k stars 810 forks source link

Replace `id` with `name` in Datatable list view. #412

Closed rebootcode closed 7 years ago

rebootcode commented 7 years ago

I have certain article table and with user id option.

In Article page, i see list of all user id, but i want to see "name" of user instead of "id".

How can this be done.

** No foreign key used.

mitulgolakiya commented 7 years ago

@rebootcode Please post some more information if you still haven't found a workaround and re-open the issue.

rebootcode commented 7 years ago

Alright! what i mean was :-

The datatable populates :- title, excerpt, status , publish_date, user_id, action

But i was looking for the way, where i could replace user_id with "name" of the user from user table

Thanks

dhcmega commented 7 years ago

To the query, add the eagerload and use the select to avoid mixing fields. To the column, add the wanted field.

    public function query()
    {
        $bookings = Booking::query();

        $bookings->with('user')->select('bookings.*');

        return $this->applyScopes($bookings);
    }

'user_id' => ['name' => 'user.name', 'data' => 'user.name', 'title' => 'User'],