2020年12月31日星期四

How to paginate a table that has live search functionality?

So I have a table with live search function. It displays all data if there is no input, but for UI purposes I need to paginate() the table. This is the function that I am using for live search:

function action(Request $request)  {      if ($request->ajax()) {          $output = '';          $query = $request->get('query');          if ($query != '') {              $data = DB::table('students')                  ->where('student_id', 'like', '%'.$query.'%')                  ->orderBy('student_id', 'desc')                  ->get();                   } else {              $data = DB::table('students')                  ->orderBy('student_id', 'desc')                  ->get();          }          $total_row = $data->count();          if ($total_row > 0) {              foreach ($data as $row) {                  $output .= '                      <tr>                          <td>'. --- .'</td>                          <td>'. --- .'</td>                      </tr>                  ';              }          }          $data = array(              'table_data'  => $output,              'total_data'  => $total_row          );          echo json_encode($data);      }  }  

This piece of code in above function displays all data when there is no input in search bar:

        else {              $data = DB::table('students')                  ->orderBy('student_id', 'desc')                  ->get();          }  

I tried adding ->paginate() before and after orderBy() and get(), but I am getting error. The only way I could use ->paginate() is without orderBy() and get(), like this: $data = DB::table('students')->paginate(), but I want to use orderBy(). If you know how, please help.

https://stackoverflow.com/questions/65521296/how-to-paginate-a-table-that-has-live-search-functionality December 31, 2020 at 09:08PM

没有评论:

发表评论