title: Extending Base Datatables Class For Crud Class
body: <p>I am using the yajra laravel datatables package.</p>
  
  <p>I'm trying to figure out how I can refactor this little bit of controller code to an individual class (UsersDataTable) which I am using method injection to make accessible. This class extends the base DataTable class but I have not added any additional functionality to this class yet. This is what I need assistance undertanding. </p>
  
  <pre class="lang-php prettyprint-override"><code>    public function index(Request $request, UsersDataTable $table, UserFilters $requestFilter)
      {
          $this-&gt;authorize('viewList', User::class);
  
          if ($request-&gt;ajax()) {
              $query = User::with('employment');
              $requestFilter-&gt;apply($query);
  
              return $table-&gt;eloquent($query)
                  -&gt;addColumn('action', 'users.partials.action-cell')
                  -&gt;filterColumn('name', function ($query, $keyword) {
                      $sql = "CONCAT(users.first_name, ' ', users.last_name)  like ?";
                      $query-&gt;whereRaw($sql, ["%{$keyword}%"]);
                  })
                  -&gt;filterColumn('id', function ($query, $keyword) {
                      $query-&gt;where($query-&gt;qualifyColumn('id'), $keyword);
                  })
                  -&gt;toJson();
          }
  
          return view('users.index');
      }
  </code></pre>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  namespace App\DataTables;
  
  use Yajra\DataTables\DataTables;
  
  class UsersDataTable extends DataTables
  {
  
  }
  </code></pre>
  
  <pre class="lang-html prettyprint-override"><code>&lt;!--begin: Datatable --&gt;
  &lt;table id="users_table" data-table="users.index" class="table table-hover"&gt;&lt;/table&gt;
  </code></pre>
  
  <pre class="lang-js prettyprint-override"><code>const table = $('[data-table="users.index"]');
  // begin first table
  table.DataTable({
      ajax: {
          url: window.location.href,
          data(params) {
              params.status = filterData.status;
              params.started_at = filterData.started_at;
          },
          error: function(xhr, error, code) {
              console.log(JSON.parse(xhr.responseText.errors));
              console.log(xhr);
              console.log(error);
              new Noty({
                  type: "error",
                  layout: "topRight",
                  text: JSON.parse(xhr.responseText.errors)
              }).show();
          }
      },
      columns: [
          { data: "id", title: "User ID" },
          { data: "name", title: "Name" },
          { data: "hometown", title: "Hometown" },
          {
              data: "employment.started_at",
              title: "Date Started",
              searchable: false
          },
          { data: "status", title: "Status", searchable: false },
          {
              data: "action",
              title: "Action",
              orderable: false,
              responsivePriority: -1
          }
      ],
      initComplete(settings) {
          rowCounter.html(`${settings.fnRecordsTotal()} Total`);
      }
  });
  </code></pre>
  
  <p>My expected results is that my controller code can me refactored to go inside of the UsersDataTable class.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I am working on what was provided to me below however, I have since decided to separate the view from its API to get the collection of users for the table to use so I won't need to create the table with the package. The problem is none of the code in the dataTable method is being ran. What am I doing wrong?</p>
  
  <p>So currently as an update, I have the following.</p>
  
  <pre><code>public function index(UsersDataTable $dataTable, UserFilters $requestFilter)
  {
      $this-&gt;authorize('viewList', User::class);
  
      $query = User::query();
      $requestFilter-&gt;apply($query);
  
      return $dataTable-&gt;eloquent($query)-&gt;toJson();
  }
  
  
  
  &lt;?php
  
  namespace App\DataTables;
  
  use App\Models\User;
  use App\Filters\UserFilters;
  use Yajra\DataTables\DataTables;
  
  class UsersDataTable extends DataTables
  {
      /** @var userFilters */
      private $userFilters;
  
      /**
       * UserDataTable constructor.
       *
       * @param UserFilters $userFilters
       */
      public function __construct(UserFilters $userFilters)
      {
          $this-&gt;userFilters = $userFilters;
      }
  
      /**
       * Build DataTable class.
       *
       * @param mixed $query Results from query() method.
       * @return \Yajra\DataTables\DataTableAbstract
       */
       public function dataTable($query)
       {
           return datatables($query)
              -&gt;editColumn('started_at', function (User $user) {
                  return $user-&gt;currentEmployment-&gt;started_at-&gt;format('Y-m-d H:s');
          })
               -&gt;editColumn('name', function (User $user) {
                  return $user-&gt;full_name;
              })
              -&gt;filterColumn('id', function ($query, $keyword) {
                  $query-&gt;where($query-&gt;qualifyColumn('id'), $keyword);
              })
              -&gt;filterColumn('name', function ($query, $keyword) {
                  $sql = "CONCAT(users.first_name, ' ', users.last_name)  like ?";
                  $query-&gt;whereRaw($sql, ["%{$keyword}%"]);
              })
              -&gt;addColumn('action', 'users.partials.action-cell');
      }
  
      /**
       * Get query source of dataTable.
       *
       * @return \Illuminate\Database\Eloquent\Builder
       */
      public function query($builder)
      {
          $query = User::with('employment');
  
          $this-&gt;userFilters-&gt;apply($query);
  
          return $query;
      }
  }
  </code></pre>