检查数据是否更新于 24 小时前或一天前 (laravel)

发布于 2025-01-17 22:11:46 字数 5402 浏览 0 评论 0原文

我可以使用碳获取数据表中每一行的几天前信息。 (查看 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 看起来像这样

Dattable

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

Dattable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光的暖冬 2025-01-24 22:11:46

由于您正在控制器中渲染表,因此您可以根据条件使用三元运算符将类名称加载,

 <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语法

<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>

Since you are rendering table in controller,so you can use ternary operator to dynamically load class name based on condition

 <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">

i suggest you to move html content from controller to blade so you can easily use blade syntax

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文