Step 01: Setting up route for show() method in PollController in routes/api.php
Route::get( '/polls', [PollController::class,'index']);
Step 02: Adding show() method in PollController
namespace App\Http\Controllers; use App\Models\Poll; use Illuminate\Http\Request; class PollController extends Controller { // public function index() { return response()->json(Poll::get(),200); } public function show($id) { return response()->json(Poll::find($id),200); } }
Step 03: Changing show() method with validation of missing record if query does not find any poll
public function show($id) { $poll = Poll::find($id); if(is_null($poll)){ return response()->json(null,404); } return response()->json($poll,200); }
For above, if poll is not found response will return empty json array {} like this with response code of 404 which means not found.
Now in browser if url is browsed like below this in local server this will a specific given poll that are in database
http://localhost:8000/api/polls/2