Browsing Poll records in restful api

Step 01: making PollController with artisan command

php artisan make:controller PollController


Step 02:
setting up route for index method in PollController in routes/api.php

Route::get( '/polls', [PollController::class,'index']);


Step 03:
finally adding index() 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);
    }    
}

Now in browser if url is browsed like below in local server this will show all the polls that are in database as json response.


http://localhost:8000/api/polls

Related Posts


Building RESTful APIs in Laravel

Seeding Poll data

Seeding Question data for a poll

Pagination for poll in restful api

Uploading image in restful api