Skip to content
Advertisement

Check if data updated_at 24 hours ago OR a day ago (laravel)

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

Advertisement

Answer

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>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement