Di artikel kali ini saya akan membahas mengenai Menggunakan Datatable Di Laravel 5, sebelum artikel ini di mulai di harapkan teman-teman telah menginstal datatable di project laravel kalian karena saya disini hanya mencontohkan penggunaanya saja.
Bagus kita seketika mulai saja cara menggunakan datatable di laracel 5, sebelum memulai saya telah menyediakan table bernama article dengan field selaku berikut :
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public function up()
{
Schema::create(‘article’, function (Blueprint $table) {
$table–>increments(‘id’);
$table–>integer(‘category_id’)–>unsigned();
$table–>string(‘title’);
$table–>string(‘news’);
$table–>string(‘images’);
$table–>timestamps();
$table–>foreign(‘category_id’)–>references(‘id’)–>on(‘category’)–>onDelete(‘cascade’);
});
}
|
Tetapi saya hanya akan menampilkan beberapa field saja selaku berikut :
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<table id=“allpost” class=“table table-striped table-bordered table-list”>
<thead>
<tr>
<th>id</th>
<th>category_id</th>
<th>title</th>
<th>created_at</th>
<th>action</th>
</tr>
</thead>
</table>
|
Baiklah kita seketika mulai saja bagi menampilkan datanya,
Bikin controller dengan method selaku berikut :
|
public function GetAllPost()
{
$article = Article::select([‘id’, ‘category_id’, ‘title’, ‘created_at’, ‘updated_at’]);
return Datatables::of($article)
–>addColumn(‘action’, function ($article) {
return ‘<a href=”/admin/editpost/’.$article–>id.‘” class=”btn btn-xs btn-primary”><i class=”glyphicon glyphicon-edit”></i> Modifikasi</a> <form action=”/admin/deletepost/’.$article–>id.‘” method=”POST”> ‘.csrf_field().‘ <button type=”submit” class=”btn btn-xs btn-danger”><i class=”glyphicon glyphicon-delete”>X</i> Delete</button></form>’;
})
–>make(true);
}
|
Dapat di lihat di ats saya pun menambahkan dua buah button di colom action
Call datatable :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(Visited 12 times, 1 visits today)
|