Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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