Skip to content

Instantly share code, notes, and snippets.

@ilyasozkurt
Created September 24, 2021 08:35
Show Gist options
  • Save ilyasozkurt/ed84735a63bfb98e0ce22c64521f5759 to your computer and use it in GitHub Desktop.
Save ilyasozkurt/ed84735a63bfb98e0ce22c64521f5759 to your computer and use it in GitHub Desktop.
<?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