3

I need ID attribute in HTML table of Laravel Datatable (https://github.com/yajra/laravel-datatables) like below.

enter image description here

I am using Laravel 5.4 and Datatable 7.x. I am using AdminLTE also. My controller is like below. I need the ID attribute.

UsersController.php

<?php
namespace App\Http\Controllers;
use DB;
use Validator;
use Datatables;
use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Users;

class UsersController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }    
    /**
     * Show the application dashboard.
     *
     * @return Response
     */
    public function index()
    {
        return view('adminlte::users');
    }    
    /**
     * return data for dashboard.
     *
     * @return Response
     */
    public function get_users()
    {
        $users = Users::select(['id','name', 'user_name', 'email',]);

        return Datatables::of($users)
            ->addColumn('action', function ($users) {
                return '<button class="edit-modal btn btn-xs btn-primary">
                            <span class="glyphicon glyphicon-edit"></span> Edit
                        </button>';
            })
            ->make(true);
    }    
}

My JavaScript code is like below.

<script type="text/javascript">
    //Display datatable
    $(function() {
        $('#users').DataTable({
            processing: true,
            serverSide: true,
            ajax: "{{ route('get_users') }}",
            columns: [
                {data: 'id', name: 'id'},
                {data: 'name', name: 'name'},
                {data: 'email', name: 'email'},
                {data: 'action', name: 'action', orderable: false, searchable: false}
            ]
        });
    });
</script>

How can I get the output ?

3
  • 1
    I m not clear what u mean but is that what u want? yajrabox.com/docs/laravel-datatables/master/row-options#row-id Commented Aug 14, 2017 at 8:08
  • Thanks for your reply @YeLwinSoe. This one is working for me ->setRowId('id'). Thanks Commented Aug 14, 2017 at 8:18
  • 1
    glad it helps u. Commented Aug 14, 2017 at 8:22

1 Answer 1

1

Use this one

return Datatables::of($posts)
         ->setRowId('post_id')
         ->make(true);

Ref: https://yajrabox.com/docs/laravel-datatables/master/row-options#row-id

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.