Donations Appreciated

When we give cheerfully and accept gratefully, everyone is blessed. - Maya Angelou

Click here to lend your support to: Laravel Datatables and make a donation at pledgie.com !

RelationController.php - Has Many Demo

Id Name Email Post

RelationController.php

    public function getHasOne(Request $request)
    {
        if ($request->ajax()) {
            $query = User::with('posts')->select('users.*');

            return $this->dataTable
                ->eloquent($query)
                ->addColumn('title', function (User $user) {
                    return $user->posts->map(function($post) {
                        return str_limit($post->title, 30, '...');
                    })->implode('<br>');
                })
                ->make(true);
        }

        return view('datatables.relation.has-one', [
            'title' => 'Has Many Eager Loading Demo',
            'controller' => 'Relation Controller',
        ]);
    }

Javascript

    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: 'https://datatables.yajrabox.com/relation/has-many',
        columns: [
            {data: 'id', name: 'users.id'},
            {data: 'name', name: 'users.name'},
            {data: 'email', name: 'users.email'},
            {data: 'title', name: 'posts.title'},
        ]
    });