Last active
June 12, 2018 08:38
-
-
Save ryanhs/272229eef593e8edadab3f9eb277356f to your computer and use it in GitHub Desktop.
laravel datatable cacher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
class BrowseController extends Controller | |
{ | |
// dattable cacher | |
public function datatable(Request $request) | |
{ | |
// dd($this->datatable1($request)); | |
$input = $request->all(); | |
$cacheInput = $request->except(['_', 'draw']); | |
$cacheKey = 'browse.'.\Auth::user()->id.'datatable.'.md5(json_encode($cacheInput)); | |
$_this = $this; | |
$output = \Cache::remember($cacheKey, 1, \Closure::fromCallable([$this, '_datatable'])); | |
$output['draw'] = $input['draw']; | |
$output['input'] = $input; | |
return $output; | |
} | |
/** | |
* Display datatable | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function _datatable() | |
{ | |
$q = \App\PpdbRegistration::select('id, a, b, c'); | |
return \DataTables::of($q) | |
->filter(function ($query) { | |
if (\Request::has('search.value')) { | |
$query->where(function($query) { | |
$qv = \Request::input('search.value'); | |
$query->orWhere('a', 'like', "%{$qv}%"); | |
}); | |
} | |
}) | |
->orderColumn('id', 'id $1') | |
// . | |
// . | |
// . | |
->toArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment