This project has been archived. If you're curious, check out one of my latest courses:

If you work with JavaScript, check out the fetch use cases and JavaScript projects.

Thank you! — Jad Joubran

API routes

In this step, we'll write the controller method that will return the list of all posts

Controller setup

Let's start by generating the controller:

php artisan make:controller PostsController

Register in the endpoint in routes/api.php

Route::get('/posts', 'PostsController@index');

Controller method

<?php

namespace App\Http\Controllers;

use App\Post;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PostsController extends Controller
{
    public function index()
    {
        $posts = Post::get();

        return response()->success(compact('posts'));
    }
}