yajra / laravel-datatables-docs

Laravel DaTatables package documentation
https://yajrabox.com/docs/laravel-datatables
93 stars 611 forks source link

Quick Starter - Installing Laravel & DataTables #81

Open bashiirmahad opened 1 year ago

bashiirmahad commented 1 year ago

In the below method I want to return joined tables data. is there any documentation to refere please

public function query(User $model): QueryBuilder
    {
        return $model->newQuery();
    }

The example of the joint

$users = DB::table('users')
        ->join('businesses', 'users.business_id', '=', 'businesses.id')
        ->select('users.id','users.name', 'users.email', 'businesses.company_name as business')
        ->get();
        return $users->newQuery();
yajra commented 1 year ago

Sorry, no documentation atm but I will be working on a new demo site soon.

Anyways, join should be the same with how the query builder works:

    public function query(User $model): QueryBuilder
    {
        return $model->newQuery()
                     ->join('businesses', 'users.business_id', '=', 'businesses.id')
                     ->select('users.id', 'users.name', 'users.email', 'businesses.company_name as business');
    }
nawt12 commented 1 year ago

I Have a eloquent solution hope useful

UserDatatable.php `

...
public function dataTable(QueryBuilder $query): EloquentDataTable
    {
        return (new EloquentDataTable($query))
 //Relationship Code
            ->addColumn('UserName', function (User $user) {
                return $user->UserDetail->UserName;
});
    }

....

  /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::make('id'),
            Column::make('UserName')->title('Name')
];
    }

`