检查数据是否 updated_at 24 小时前或一天前 (laravel)
Check if data updated_at 24 hours ago OR a day ago (laravel)
我可以使用 carbon 获取 dattable 中每一行的几天前信息。 (查看 CustomController.php)
但在用户能够编辑数据之前。我想检查数据是一天前还是 24 小时前更新的。
如果 24 小时或一天过去了用户无法更新数据如果在 24 小时范围内则用户可以更新数据
迁移
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>
数据表看起来像这样
由于您在控制器中渲染 table,因此您可以使用三元运算符根据条件动态加载 class 名称
<a href="#" id="' . $session->id . '" class="text-success mx-1 editIcon '.( $session->created_at->diffInHours(now())>24?"disable-link":null).'" data-toggle="modal" data-target="#editSessionModal">
我建议您将 html 内容从控制器移动到 blade 以便您可以轻松使用 blade 语法
<a href="#" id="{{ $session->id}}" class="text-success mx-1 editIcon @if( $session->created_at->diffInDays(now())>24)disable-link @endif " data-toggle="modal" data-target="#editSessionModal"><i class="bi-pencil-square h4"></i></a>
我可以使用 carbon 获取 dattable 中每一行的几天前信息。 (查看 CustomController.php)
但在用户能够编辑数据之前。我想检查数据是一天前还是 24 小时前更新的。 如果 24 小时或一天过去了用户无法更新数据如果在 24 小时范围内则用户可以更新数据
迁移
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>
数据表看起来像这样
由于您在控制器中渲染 table,因此您可以使用三元运算符根据条件动态加载 class 名称
<a href="#" id="' . $session->id . '" class="text-success mx-1 editIcon '.( $session->created_at->diffInHours(now())>24?"disable-link":null).'" data-toggle="modal" data-target="#editSessionModal">
我建议您将 html 内容从控制器移动到 blade 以便您可以轻松使用 blade 语法
<a href="#" id="{{ $session->id}}" class="text-success mx-1 editIcon @if( $session->created_at->diffInDays(now())>24)disable-link @endif " data-toggle="modal" data-target="#editSessionModal"><i class="bi-pencil-square h4"></i></a>