-
-
Save ilyasozkurt/ed84735a63bfb98e0ce22c64521f5759 to your computer and use it in GitHub Desktop.
This file contains 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 App\Models\Earthquake; | |
use Illuminate\Http\Request; | |
class EarthquakeController extends Controller | |
{ | |
public function index(Request $request) | |
{ | |
//Accessing earthquake model instance to run mysql queries. | |
$response = Earthquake::orderBy('happened_at', 'desc') | |
->when($request->get('search'), function ($query, $search) { | |
//When request has search parameter apply this filter. | |
$query->where('region', 'like', '%' . $search . '%'); | |
})->simplePaginate();//Automatically creates pagination | |
//return this model object as response. Laravel will convert this object to json response automatically. | |
return response($response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment