In this post, you'll be learning how to implement JSON Feed in Laravel. The steps are very simple and straightforward, so do follow along and you will have it implemented in no time.
To implement this, create a new route and call it "/json-feed". This will be the URL that's accessible by the user and let's define the logic in the route body. Do note that you can create a separate controller for this if you prefer to do so.
Define The Route
To implement this, create a new route and call it "/json-feed". This will be the URL that's accessible by the user and let's define the logic in the route body. Do note that you can create a separate controller for this if you prefer to do so.
Route::get('json-feed', function () { /* route body */ });
Query the model
Inside the route body query the model for the feed which in the case of this tutorial, it's the "post" model.
$posts = Post::query() ->orderByDesc('published_at') ->limit(20) ->get();
Define the main structure
The next step is to define the main structure of the JSON feed. Following the specification, it will look like below. Do make sure to update the data with your own website details.
$data = [ 'version' => 'https://jsonfeed.org/version/1', 'title' => 'Your App Name', 'description' => 'Your App Description', 'home_page_url' => 'https://your-app-here.test', 'feed_url' => 'https://your-app-here.test/json-feed', 'icon' => 'https://your-app-here.test/img/logo.png', 'favicon' => 'https://your-app-here.test/favicon.png', 'items' => [], ];
Define the items to iterate
With the posts that we have defined previously, now it's time to add them to the items key of the main structure. With Laravel collection, do call the map method and then create the associative array as follows. Lastly, return the whole data as the response and by default, Laravel will return it as JSON.
$data['items'] = $posts->map(fn($post) => [ 'id' => $post->id, 'title' => $post->title, 'url' => 'https://your-app-here.test/posts/' . $post->slug, 'image' => 'https://your-app-here.test/' . $post->cover, 'content_html' => $post->body, 'date_created' => $post->published_at->tz('UTC')->toRfc3339String(), 'date_modified' => $post->updated_at->tz('UTC')->toRfc3339String(), 'author' => [ 'name' => $post->user->name ], ]); return $data;
Final Code
Below is the final code for the whole steps above and you can pretty much create JSON feed with different models altogether or separately into different routes. You can further implement caching to better optimize the retrieval if it's necessary for you.
Route::get('json-feed', function () { $posts = Post::query() ->orderByDesc('published_at') ->limit(20) ->get(); $data = [ 'version' => 'https://jsonfeed.org/version/1', 'title' => 'Your App Name', 'description' => 'Your App Description', 'home_page_url' => 'https://your-app-here.test', 'feed_url' => 'https://your-app-here.test/json-feed', 'icon' => 'https://your-app-here.test/img/logo.png', 'favicon' => 'https://your-app-here.test/favicon.png', 'items' => [], ]; $data['items'] = $posts->map(fn($post) => [ 'id' => $post->id, 'title' => $post->title, 'url' => 'https://your-app-here.test/posts/' . $post->slug, 'image' => 'https://your-app-here.test/' . $post->cover, 'content_html' => $post->body, 'date_created' => $post->published_at->tz('UTC')->toRfc3339String(), 'date_modified' => $post->updated_at->tz('UTC')->toRfc3339String(), 'author' => [ 'name' => $post->user->name ], ]); return $data; });
I hope this tutorial is helpful for you and if there's any question, do comment out below and let's start the discussion. Also, don't forget to share it with your friend as well and I would appreciate it a lot. Cheers and have a good try.
Leave a reply