检查数据是否更新于 24 小时前或一天前 (laravel)
我可以使用碳获取数据表中每一行的几天前信息。 (查看 CustomController.php)
但在用户能够编辑数据之前。我想检查数据是一天前还是24小时前更新的。 如果 24 小时或一天过去了用户无法更新数据(如果在 24 小时范围内),则用户可以更新数据
Migration
public function up()
{
Schema::create('table_sessions', function (Blueprint $table) {
$table->id();
$table->string('session')->unique();
$table->timestamps();
});
}
CustomController.php
public function EditSession(Request $request)
{
$id = $request->id;
$session = SessionModel::find($id);
return response()->json($session);
}
public function FetchSession()
{
$session = SessionModel::all();
$output = '';
if ($session->count() > 0) {
$output .= '<table class="table text-center align-middle table-flush table-hover">
<thead class="thead-light">
<tr>
<th width="50px"><input type="checkbox" class="chk" name="main_checkbox" id="master"></th>
<th width="10%">ID</th>
<th>Session</th>
<th width="50%">Action</th>
<th>Updated On</th>
</tr>
</thead>
<tbody>';
foreach ($session as $session) {
$output .= '<tr>
<td><input type="checkbox" id="sub_master" name="session_checkbox" class="sub_chk" data-id="' . $session->id . '"></td>
<td>' . $session->id . '</td>
<td>' . $session->session . '</td>
<td>
<a href="#" id="' . $session->id . '" class="text-success mx-1 editIcon" data-toggle="modal" data-target="#editSessionModal"><i class="bi-pencil-square h4"></i></a>
<!-- Edit Button -->
<a href="#" id="' . $session->id . '" class="text-danger mx-1 deleteIcon"><i class="bi-trash h4"></i></a>
</td>
<td scope="row">
<label id="timeLabel">' . $session->updated_at->diffInDays(Carbon::now()) . ' days ago</label>
<!-- **getting days ago using this code** -->
</td>
</tr>';
}
$output .= '</tbody></table>';
echo $output;
} else {
echo '<h1 class="text-center text-shifondary my-5">No record present in the database!</h1>';
}
}
Web .php
Route::get('fetch-sessions', [CustomController::class, 'FetchSession'])->name('session.fetch.route');
Route::get('edit-session', [CustomController::class, 'EditSession'])->name('session.edit.route');
view.blade.php
<div class="table-responsive p-3" id="show_all_sessions">
</div>
{{-- edit Modal --}}
<div class="modal-body">
<form action="#" autocomplete="off" method="POST" id="edit_session_form" enctype="multipart/form-data">
@csrf
<input type="hidden" name="session_id" id="session_id">
<input type="" name="session_time" id="session_time">
<div class="form-group">
<label for="session">Session <span class="text-danger">*</span></label>
<div class="controls">
<input id="session_i" name="session_i" required class="form-control" placeholder="Enter Session Name">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-danger mb-1" data-dismiss="modal">Cancel</button>
<button type="submit" id="edit_session_btn" class="btn btn-secondary mb-1">Update</button>
</div>
</form>
</div>
{{-- //Scripts --}}
<script type="text/javascript">
$(document).ready(function() {
function fetchAllSessions() {
var _url = '{{ route("session.fetch.rn") }}';
$.ajax({
url: _url,
method: 'get',
success: function(response) {
$("#show_all_sessions").html(response);
$("table").DataTable();
}
});
}
// edit session ajax request for edit button on datatable
$(document).on('click', '.editIcon', function(e) {
e.preventDefault();
let id = $(this).attr('id');
//let time = $("#timeLabel").attr('text');
var _url = '{{ route("session.edit.route") }}';
$.ajax({
url: _url,
method: 'get',
data: {
id: id,
_token: '{{ csrf_token() }}'
},
success: function(response) {
// here some logic tocheck if clicked data updated 24 hours ago or a day ago using if else.
$("#session_id").val(response.id);
$("#session_i").val(response.session);
}
});
});
});
</script>
DataTable 看起来像这样
I am able to get days ago for each row in dattable using carbon. (view CustomController.php)
But before the user is able to edit the data. I want to check if the data is updated a day ago or 24 hours ago.
if 24 hours or a day passed user can't update the data if it is within 24 hours range then the user can update the data
Migration
public function up()
{
Schema::create('table_sessions', function (Blueprint $table) {
$table->id();
$table->string('session')->unique();
$table->timestamps();
});
}
CustomController.php
public function EditSession(Request $request)
{
$id = $request->id;
$session = SessionModel::find($id);
return response()->json($session);
}
public function FetchSession()
{
$session = SessionModel::all();
$output = '';
if ($session->count() > 0) {
$output .= '<table class="table text-center align-middle table-flush table-hover">
<thead class="thead-light">
<tr>
<th width="50px"><input type="checkbox" class="chk" name="main_checkbox" id="master"></th>
<th width="10%">ID</th>
<th>Session</th>
<th width="50%">Action</th>
<th>Updated On</th>
</tr>
</thead>
<tbody>';
foreach ($session as $session) {
$output .= '<tr>
<td><input type="checkbox" id="sub_master" name="session_checkbox" class="sub_chk" data-id="' . $session->id . '"></td>
<td>' . $session->id . '</td>
<td>' . $session->session . '</td>
<td>
<a href="#" id="' . $session->id . '" class="text-success mx-1 editIcon" data-toggle="modal" data-target="#editSessionModal"><i class="bi-pencil-square h4"></i></a>
<!-- Edit Button -->
<a href="#" id="' . $session->id . '" class="text-danger mx-1 deleteIcon"><i class="bi-trash h4"></i></a>
</td>
<td scope="row">
<label id="timeLabel">' . $session->updated_at->diffInDays(Carbon::now()) . ' days ago</label>
<!-- **getting days ago using this code** -->
</td>
</tr>';
}
$output .= '</tbody></table>';
echo $output;
} else {
echo '<h1 class="text-center text-shifondary my-5">No record present in the database!</h1>';
}
}
Web.php
Route::get('fetch-sessions', [CustomController::class, 'FetchSession'])->name('session.fetch.route');
Route::get('edit-session', [CustomController::class, 'EditSession'])->name('session.edit.route');
view.blade.php
<div class="table-responsive p-3" id="show_all_sessions">
</div>
{{-- edit Modal --}}
<div class="modal-body">
<form action="#" autocomplete="off" method="POST" id="edit_session_form" enctype="multipart/form-data">
@csrf
<input type="hidden" name="session_id" id="session_id">
<input type="" name="session_time" id="session_time">
<div class="form-group">
<label for="session">Session <span class="text-danger">*</span></label>
<div class="controls">
<input id="session_i" name="session_i" required class="form-control" placeholder="Enter Session Name">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-danger mb-1" data-dismiss="modal">Cancel</button>
<button type="submit" id="edit_session_btn" class="btn btn-secondary mb-1">Update</button>
</div>
</form>
</div>
{{-- //Scripts --}}
<script type="text/javascript">
$(document).ready(function() {
function fetchAllSessions() {
var _url = '{{ route("session.fetch.rn") }}';
$.ajax({
url: _url,
method: 'get',
success: function(response) {
$("#show_all_sessions").html(response);
$("table").DataTable();
}
});
}
// edit session ajax request for edit button on datatable
$(document).on('click', '.editIcon', function(e) {
e.preventDefault();
let id = $(this).attr('id');
//let time = $("#timeLabel").attr('text');
var _url = '{{ route("session.edit.route") }}';
$.ajax({
url: _url,
method: 'get',
data: {
id: id,
_token: '{{ csrf_token() }}'
},
success: function(response) {
// here some logic tocheck if clicked data updated 24 hours ago or a day ago using if else.
$("#session_id").val(response.id);
$("#session_i").val(response.session);
}
});
});
});
</script>
DataTable looks like this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您正在控制器中渲染表,因此您可以根据条件使用三元运算符将类名称加载,
我建议您将HTML内容从控制器移动到刀片,以便您可以轻松地使用Blade语法
Since you are rendering table in controller,so you can use ternary operator to dynamically load class name based on condition
i suggest you to move html content from controller to blade so you can easily use blade syntax