Donations Appreciated

It's not how much we give but how much love we put into giving. - Mother Teresa

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

RelationController.php - Morph To Many Demo

NOTE: MorphToMany only works for search. Sorting is subjective since it will do nothing if the orderable column is from a morphToMany relationship.
Id Title Tag

RelationController.php

    public function getMorphToMany(Request $request)
    {
        if ($request->ajax()) {
            $query = Post::with('tags')->select('posts.*');

            return $this->dataTable->eloquent($query)
                ->addColumn('tags', function (Post $post) {
                    return $post->tags->pluck('name')->implode('<br>');
                })
                ->make(true);
        }

        return view('datatables.relation.morph-to-many', [
            'title'      => 'Morph To Many Demo',
            'controller' => 'Relation Controller',
        ]);
    }

Javascript

    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '',
        columns: [
            {data: 'id', name: 'posts.id'},
            {data: 'title', name: 'posts.title'},
            {data: 'tags', name: 'tags.name'},
        ]
    });