Create Laravel JSON Fake REST APIs Server via JSON File

What is Laravel Fake JSON REST APIs Server

How to Install Setup

Clone github repository

$ git clone https://github.com/owthub/laravel-json-fake-apis-server-using-json-file.git
Cloning into 'laravel-json-fake-apis-server-using-json-file'... remote: Enumerating objects: 889, done. remote: Counting objects: 100% (889/889), done. remote: Compressing objects: 100% (786/786), done. remote: Total 889 (delta 82), reused 889 (delta 82), pack-reused 0 Receiving objects: 100% (889/889), 6.12 MiB | 205.00 KiB/s, done. Resolving deltas: 100% (82/82), done. Checking out files: 100% (844/844), done.
$ php artisan serve
http://localhost/laravel-json-fake-apis-server-using-json-file

Create db.json File

How To Create

{ "students": [ ], "posts": [ ], "comments": [ ] }
{ "students": [ { "id": 1, "name": "Sanjay Kumar", "email": "sanjay@gmail.com" }, { "id": 2, "name": "Vijay Rohila", "email": "vijay@gmail.com" }, { "id": 3, "name": "Dhananjay Negi", "email": "dhananjay@gmail.com" }, { "id": 4, "name": "Ashish Kumar", "email": "ashish@gmail.com" }, ... ], "posts": [ { "id": 1, "title": "Sample Post 1", "description": "This is sample post 1" }, { "id": 2, "title": "Sample Post 2", "description": "This is sample post 2" }, ... ], "comments": [ { "id": 1, "title": "Sample comment 1", "description": "This is sample comment 1" }, { "id": 2, "title": "Sample comment 2", "description": "This is sample comment 2" }, ... ] }
  • URL/students — List all data
  • URL/posts
  • URL/comments
  • URL/students/{studentId} OR URL/students?id={studentId} — List single data
  • URL/posts/{postId} OR URL/posts?id={postId}
  • URL/comments/{commentId} OR URL/comments?id={commentId}
  • A Student have many posts
  • A Post have many comments
{singularResourceName}Id
studentId, postId
{ "students":[ { "id": 1, "name": "Sanjay Kumar", "email": "sanjay@gmail.com" }, ... ], "posts":[ { "id": 1, "studentId": 1, "title": "Sample post 1", "description": "This is sample post 1" }, { "id": 2, "studentId": 1, "title": "Sample post 2", "description": "This is sample post 2" }, { "id": 3, "studentId": 2, "title": "Sample post 3", "description": "This is sample post 3" } ... ], "comments":[ { "id": 1, "postId": 1, "title": "Sample comment 1", "description": "This is sample comment 1" }, { "id": 2, "postId": 1, "title": "Sample comment 2", "description": "This is sample comment 2" }, ... ] }
  • URL/students/{studentId}/posts — List all posts by student id
  • URL/students/posts?studentId={studentId}
  • URL/posts/{postId}/comments — List all comments by post id
  • URL/posts/comments?postId={postId}

Final Words

{ "students": [], "posts": [], ... }{ "students": [ { "id": 1, "name": "Sample" }, { "id": 2, "name": "Sample" } ], ... }
  • Relationship between resources as “id” for their tables and {singular}Id as for others to connect. Like “id” inside students, but for posts it will be “studentId” which connects two resources students and posts.
  • db.json file should placed inside /storage/json folder.
  • Resouces names should be plural.
  • When you execute POST, PUT/PATCH, DELETE request with fake apis, they will execute even they will return a perfect json output but they don’t do anything at server. Means they don’t save, update or delete anything in real.

--

--

--

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Online Web Tutor

Online Web Tutor

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors

More from Medium

Laravel Advance | Laravel Notification Alert Tutorial | Bootstrap Notify Plugin Example

Laravel Livewire | Click Event Example

Laravel Livewire | Toastr Notifications Example

Laravel Livewire | Pagination Example