Donations Appreciated

The simplest acts of kindness are by far more powerful then a thousand heads bowing in prayer. - Mahatma Gandhi

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

RelationController.php - Has One Demo

Id Name Email Post

RelationController.php

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

            return $this->dataTable
                ->eloquent($query)
                ->addColumn('title', function (User $user) {
                    return $user->onePost ? str_limit($user->onePost->title, 30, '...') : '';
                })
                ->make(true);
        }

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

Javascript

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