@@ -29,16 +29,15 @@ use Yajra\Datatables\Services\DataTable;
2929class PostsDataTable extends DataTable
3030{
3131 /**
32- * Display ajax response .
32+ * Build DataTable class .
3333 *
34- * @return \Illuminate\Http\JsonResponse
34+ * @return \Yajra\Datatables\Engines\BaseEngine
3535 */
36- public function ajax ()
36+ public function dataTable ()
3737 {
3838 return $this->datatables
3939 ->eloquent($this->query())
40- ->addColumn('action', 'path.to.action.view')
41- ->make(true);
40+ ->addColumn('action', 'path.to.action.view');
4241 }
4342
4443 /**
@@ -99,9 +98,88 @@ class PostsDataTable extends DataTable
9998In this example, we will pass a ` --model ` option to set the model to be used by our DataTable.
10099
101100```
102- php artisan datatables:make PostsDataTable --model=Post
101+ php artisan datatables:make Posts --model
103102```
104103
104+ This will generate a ` App\DataTables\PostsDataTable ` class that uses ` App\Post ` as the base model for our query.
105+ The exported filename will also be set to ` posts_(timestamp) ` .
106+
107+ ``` php
108+ <?php
109+
110+ namespace App\DataTables;
111+
112+ use App\Post;
113+ use Yajra\Datatables\Services\DataTable;
114+
115+ class PostsDataTable extends DataTable
116+ {
117+ /**
118+ * Build DataTable class.
119+ *
120+ * @return \Yajra\Datatables\Engines\BaseEngine
121+ */
122+ public function dataTable()
123+ {
124+ return $this->datatables
125+ ->eloquent($this->query())
126+ ->addColumn('action', 'path.to.action.view');
127+ }
128+
129+ /**
130+ * Get the query object to be processed by dataTables.
131+ *
132+ * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
133+ */
134+ public function query()
135+ {
136+ $query = Post::query();
137+
138+ return $this->applyScopes($query);
139+ }
140+
141+ /**
142+ * Optional method if you want to use html builder.
143+ *
144+ * @return \Yajra\Datatables\Html\Builder
145+ */
146+ public function html()
147+ {
148+ return $this->builder()
149+ ->columns($this->getColumns())
150+ ->ajax('')
151+ ->addAction(['width' => '80px'])
152+ ->parameters($this->getBuilderParameters());
153+ }
154+
155+ /**
156+ * Get columns.
157+ *
158+ * @return array
159+ */
160+ protected function getColumns()
161+ {
162+ return [
163+ 'id',
164+ // add your columns
165+ 'created_at',
166+ 'updated_at',
167+ ];
168+ }
169+
170+ /**
171+ * Get filename for export.
172+ *
173+ * @return string
174+ */
175+ protected function filename()
176+ {
177+ return 'posts_' . time();
178+ }
179+ }
180+ ```
181+
182+
105183## Creating a DataTable Scope service class
106184
107185DataTable scope is class that we can use to limit our database search results based on the defined query scopes.
0 commit comments