|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\CMS\DataTables; |
|
4
|
|
|
|
|
5
|
|
|
use Yajra\CMS\Entities\Article; |
|
6
|
|
|
use Yajra\Datatables\Services\DataTable; |
|
7
|
|
|
|
|
8
|
|
|
class ArticlesDataTable extends DataTable |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Display ajax response. |
|
12
|
|
|
* |
|
13
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
14
|
|
|
*/ |
|
15
|
|
|
public function ajax() |
|
16
|
|
|
{ |
|
17
|
|
|
return $this->datatables |
|
18
|
|
|
->eloquent($this->query()) |
|
19
|
|
|
->addColumn('action', 'administrator.articles.datatables.action') |
|
20
|
|
|
->editColumn('published', function (Article $article) { |
|
21
|
|
|
return dt_check($article->published); |
|
22
|
|
|
}) |
|
23
|
|
|
->editColumn('authenticated', function (Article $article) { |
|
24
|
|
|
return dt_check($article->authenticated); |
|
25
|
|
|
}) |
|
26
|
|
|
->editColumn('is_page', function (Article $article) { |
|
27
|
|
|
return dt_check($article->is_page); |
|
28
|
|
|
}) |
|
29
|
|
|
->editColumn('hits', function (Article $article) { |
|
30
|
|
|
return '<span class="label bg-purple">' . $article->hits . '</span>'; |
|
31
|
|
|
}) |
|
32
|
|
|
->editColumn('title', function (Article $article) { |
|
33
|
|
|
return view('administrator.articles.datatables.title', compact('article'))->render(); |
|
|
|
|
|
|
34
|
|
|
}) |
|
35
|
|
|
->addColumn('plain_title', function (Article $article) { |
|
36
|
|
|
return $article->title; |
|
37
|
|
|
}) |
|
38
|
|
|
->addColumn('slug', function (Article $article) { |
|
39
|
|
|
return $article->present()->slug; |
|
40
|
|
|
}) |
|
41
|
|
|
->rawColumns(['is_page', 'hits', 'title', 'published', 'authenticated', 'action']) |
|
42
|
|
|
->make(true); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the query object to be processed by datatables. |
|
47
|
|
|
* |
|
48
|
|
|
* @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder |
|
49
|
|
|
*/ |
|
50
|
|
|
public function query() |
|
51
|
|
|
{ |
|
52
|
|
|
$articles = Article::select('articles.*', 'categories.title as category_title') |
|
53
|
|
|
->join('categories', 'categories.id', '=', 'articles.category_id'); |
|
54
|
|
|
|
|
55
|
|
|
return $this->applyScopes($articles); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Optional method if you want to use html builder. |
|
60
|
|
|
* |
|
61
|
|
|
* @return \Yajra\Datatables\Html\Builder |
|
62
|
|
|
*/ |
|
63
|
|
|
public function html() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->builder() |
|
66
|
|
|
->columns($this->getColumns()) |
|
67
|
|
|
->ajax('') |
|
68
|
|
|
->parameters($this->getBuilderParameters()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get columns. |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
private function getColumns() |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
|
|
[ |
|
80
|
|
|
'data' => 'id', |
|
81
|
|
|
'name' => 'articles.id', |
|
82
|
|
|
'title' => trans('cms::article.datatable.columns.id'), |
|
83
|
|
|
'width' => '20px', |
|
84
|
|
|
], |
|
85
|
|
|
[ |
|
86
|
|
|
'data' => 'title', |
|
87
|
|
|
'name' => 'articles.title', |
|
88
|
|
|
'title' => trans('cms::article.datatable.columns.title'), |
|
89
|
|
|
], |
|
90
|
|
|
[ |
|
91
|
|
|
'data' => 'alias', |
|
92
|
|
|
'name' => 'articles.alias', |
|
93
|
|
|
'visible' => false, |
|
94
|
|
|
], |
|
95
|
|
|
[ |
|
96
|
|
|
'data' => 'categories.title', |
|
97
|
|
|
'title' => trans('cms::article.datatable.columns.category'), |
|
98
|
|
|
'visible' => false, |
|
99
|
|
|
'data' => 'category_title', |
|
100
|
|
|
], |
|
101
|
|
|
[ |
|
102
|
|
|
'data' => 'published', |
|
103
|
|
|
'name' => 'articles.published', |
|
104
|
|
|
'width' => '20px', |
|
105
|
|
|
'title' => '<i class="fa fa-check-circle" data-toggle="tooltip" data-title="' . trans('cms::article.datatable.columns.published') . '"></i>', |
|
106
|
|
|
], |
|
107
|
|
|
[ |
|
108
|
|
|
'data' => 'authenticated', |
|
109
|
|
|
'name' => 'articles.authenticated', |
|
110
|
|
|
'width' => '20px', |
|
111
|
|
|
'title' => '<i class="fa fa-key" data-toggle="tooltip" data-title="' . trans('cms::article.datatable.columns.authenticated') . '"></i>', |
|
112
|
|
|
], |
|
113
|
|
|
[ |
|
114
|
|
|
'data' => 'order', |
|
115
|
|
|
'name' => 'articles.order', |
|
116
|
|
|
'width' => '20px', |
|
117
|
|
|
'title' => '<i class="fa fa-list" data-toggle="tooltip" data-title="' . trans('cms::article.datatable.columns.order') . '"></i>', |
|
118
|
|
|
], |
|
119
|
|
|
[ |
|
120
|
|
|
'data' => 'hits', |
|
121
|
|
|
'name' => 'articles.hits', |
|
122
|
|
|
'width' => '20px', |
|
123
|
|
|
'title' => '<i class="fa fa-eye" data-toggle="tooltip" data-title="' . trans('cms::article.datatable.columns.hits') . '"></i>', |
|
124
|
|
|
], |
|
125
|
|
|
[ |
|
126
|
|
|
'data' => 'is_page', |
|
127
|
|
|
'name' => 'articles.is_page', |
|
128
|
|
|
'title' => trans('cms::article.datatable.columns.is_page'), |
|
129
|
|
|
], |
|
130
|
|
|
[ |
|
131
|
|
|
'data' => 'updated_at', |
|
132
|
|
|
'name' => 'articles.updated_at', |
|
133
|
|
|
'title' => trans('cms::article.datatable.columns.updated_at'), |
|
134
|
|
|
'width' => '100px', |
|
135
|
|
|
], |
|
136
|
|
|
[ |
|
137
|
|
|
'data' => 'action', |
|
138
|
|
|
'title' => trans('cms::article.datatable.columns.action'), |
|
139
|
|
|
'width' => '134px', |
|
140
|
|
|
'searchable' => false, |
|
141
|
|
|
'orderable' => false, |
|
142
|
|
|
'className' => 'text-center', |
|
143
|
|
|
], |
|
144
|
|
|
]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
|
View Code Duplication |
protected function getBuilderParameters() |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
return [ |
|
153
|
|
|
'stateSave' => true, |
|
154
|
|
|
'buttons' => [ |
|
155
|
|
|
[ |
|
156
|
|
|
'extend' => 'create', |
|
157
|
|
|
'text' => '<i class="fa fa-plus"></i> ' . trans('cms::article.datatable.buttons.create'), |
|
158
|
|
|
], |
|
159
|
|
|
'export', |
|
160
|
|
|
'print', |
|
161
|
|
|
'reset', |
|
162
|
|
|
'reload', |
|
163
|
|
|
], |
|
164
|
|
|
]; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get filename for export. |
|
169
|
|
|
* |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function filename() |
|
173
|
|
|
{ |
|
174
|
|
|
return 'articles'; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: