This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Post extends Model | |
{ | |
protected $fillable = ['title', 'content', 'user_id']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Comment extends Model | |
{ | |
protected $fillable = ['content', 'user_id']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Ajax; | |
use App\Http\Controllers\Controller; | |
use Illuminate\Http\Request; | |
use DataTables; | |
use App\Post; | |
class PostController extends Controller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container" style="margin-top: 50px"> | |
<table id="demo_table" class="table table-striped table-bordered" style="width:100%"> | |
<thead> | |
<tr> | |
<th>#</th> | |
<th>Title</th> | |
<th>User Name</th> | |
<th>Comments Number</th> | |
<th>Actions</th> | |
</tr> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready( function () { | |
$('#demo_table').DataTable({ | |
"processing": true, | |
"serverSide": true, | |
"ajax": '{{ route('ajax.posts.index') }}', | |
"columns": [ | |
{ "data": "id" }, | |
{ "data": "title" }, | |
{ "data": "user_name" }, | |
{ "data": "comments_num" }, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table id="demo_table" class="display" style="width:100%"> | |
<thead> | |
<tr> | |
<th>#</th> | |
<th>Title</th> | |
<th>User Id</th> | |
<th>Comments Number</th> | |
</tr> | |
</thead> | |
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
->addColumn('edit_url', function($row){ | |
return route('posts.edit', $row->id); | |
}) | |
->addColumn('delete_url', function($row){ | |
return route('posts.destroy', $row->id); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ data: null, | |
render: function(data){ | |
var edit_button = '<a href="' + data.edit_url + '" class="btn btn-primary" role="button" aria-pressed="true">Edit</a>'; | |
var delete_button = '<form action="' + data.delete_url + '" method="POST"><input type="hidden" name="_method" value="delete">{{csrf_field()}}<button type="submit" class="btn btn-danger">Delete</button>'; | |
return edit_button + delete_button; | |
} | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form action="{!! $deleteUrl !!}" method="POST"> | |
<input type="hidden" name="_method" value="delete"> | |
{{ csrf_field() }} | |
<button type="submit" class="btn btn-danger">Delete</button> | |
</form> | |
<a href="{!! $editUrl !!}" class="btn btn-primary" role="button" aria-pressed="true">Edit</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
->addColumn('actions', function($row){ | |
$editUrl = route('posts.edit', $row->id); | |
$deleteUrl = route('posts.destroy', $row->id); | |
return view('_formActions', compact('editUrl', 'deleteUrl')); | |
}) |
OlderNewer