Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 8169

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T10:37:10+00:00 2024-11-28T10:37:10+00:00

Simple and Easy REST API Implementation With Laravel

  • 60k

What is API?

API is a method where two or more software can communicate each other.

What is REST API

REST (Representational State Transfer) API is an architecture that allow server and client to transfer the representation of the state of the resource to the requester or endpoint. The data can be transferred using JSON, XML, or another format but the most popular option is JSON (JavaScript Object Notation). You can watch this video if you feel don't familiar with JSON format.

REST API Implementation with Laravel

Prerequisites

  • PHP 8.0+
  • Composer v2.0+
  • Postman

Project Initialization

First, let initialize our Laravel project. This should automatically install the latest version of Laravel.

composer create-project laravel/laravel rest-api-implementation-laravel cd rest-api-implementation-laravel 
Enter fullscreen mode Exit fullscreen mode

Then ensure the server can be up and running:

php artisan serve 
Enter fullscreen mode Exit fullscreen mode

Then open http://127.0.0.1:8000, you should see this sweet Laravel welcome screen:

Laravel default welcome screen

Setting Up Database

I want to make this tutorial to be as simple as possible, so we're going to use SQLite3 for our database connection.

First, install sqlite and create sqlite file:

# Change 8.1 according to your php version sudo apt install php8.1-sqlite3 sqlite3  touch database.sqlite 
Enter fullscreen mode Exit fullscreen mode

Open your .env file and change DB_CONNECTION to sqlite, then delete DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD:

DB_CONNECTION=sqlite 
Enter fullscreen mode Exit fullscreen mode

Creating Migrations and Model

We want to create model and migration for Developer table. Fortunately, Laravel provides easy way to make model and migrations with one single command:

php artisan make:model Developer --migration 
Enter fullscreen mode Exit fullscreen mode

Then, you will have two new files app/Models/Developer.php for the model and database/migrations/2022_03_18_090902_create_developers_table.php for the migrations. Note that the timestamp of migration file depends on when you execute the command.

Created model and migration files

Migrate table

<?php  use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema;  return new class extends Migration {     /**      * Run the migrations.      *      * @return void      */     public function up()     {         Schema::create('developers', function (Blueprint $table) {             $table->id();             $table->timestamps();         });     }      /**      * Reverse the migrations.      *      * @return void      */     public function down()     {         Schema::dropIfExists('developers');     } }; 
Enter fullscreen mode Exit fullscreen mode

Short explanation:

  • up() method will be called when we run the migrations
  • down() method will be called when we rollback the migrations
  • Schema::dropIfExists('developers') will drop developers table from database if exists
  • Schema::create('developers') will create developers table
  • $table->id() will create incremental primary key with name id
  • $table->timestamps() will add created_at and updated_at columns to the table

Supposed we want to add name and fav_lang columns with string type, we can modify the migration to be like this:

Schema::create('developers', function (Blueprint $table) {     $table->id();     $table->string('name');     $table->string('fav_lang');     $table->timestamps(); }); 
Enter fullscreen mode Exit fullscreen mode

Then, run the migration:

php artisan migrate 
Enter fullscreen mode Exit fullscreen mode

Creating Controller

To make simple controller for API, we can use default artisan command with --model and --api arguments:

php artisan make:controller DeveloperController --api --model=Developer 
Enter fullscreen mode Exit fullscreen mode

Here is our controller:

<?php  namespace AppHttpControllers;  use AppModelsDeveloper; use IlluminateHttpRequest;  class DeveloperController extends Controller {     /**      * Display a listing of the resource.      *      * @return IlluminateHttpResponse      */     public function index()     {         //     }      /**      * Store a newly created resource in storage.      *      * @param  IlluminateHttpRequest  $request      * @return IlluminateHttpResponse      */     public function store(Request $request)     {         //     }      /**      * Display the specified resource.      *      * @param  AppModelsDeveloper  $developer      * @return IlluminateHttpResponse      */     public function show(Developer $developer)     {         //     }      /**      * Update the specified resource in storage.      *      * @param  IlluminateHttpRequest  $request      * @param  AppModelsDeveloper  $developer      * @return IlluminateHttpResponse      */     public function update(Request $request, Developer $developer)     {         //     }      /**      * Remove the specified resource from storage.      *      * @param  AppModelsDeveloper  $developer      * @return IlluminateHttpResponse      */     public function destroy(Developer $developer)     {         //     } } 
Enter fullscreen mode Exit fullscreen mode

Look, our controller methods have $developer argument. Laravel will find record in database automatically for Developer table with Route model binding.

Setting Up Routes

Now, open routes/api.php and add DeveloperController to the route:

Route::apiResource('developers', DeveloperController::class); 
Enter fullscreen mode Exit fullscreen mode

Now check our route list:

php artisan route:list 
Enter fullscreen mode Exit fullscreen mode

Route list for Developer resource

Short explanations:

  • GET method for api/developers will be handled by index() method in DeveloperController. This route is for display a listing of the resource.
  • GET method for api/developers/{developers} will be handled by show() method in DeveloperController. This route is for display specific resource.
  • POST method for api/developers will be handled by store() method in DeveloperController. This route is for creating new resource.
  • PUT/PATCH method for api/developers/{developers} will be handled by update() method in DeveloperController. This route is for updating specific resource.
  • DELETE method for api/developers/{developers} will be handled by destroy() method in DeveloperController. This route is for deleting specific resource.

Handling Request

Creating Resource

When creating new resource, we need to take input name and fav_lang from user and put it to the database.

public function store(Request $request) {     $developer = new Developer();     $developer->name = $request->input('name');     $developer->fav_lang = $request->input('fav_lang');      $developer->save();     return response()->json([ 'message' => 'Resource created' ]); } 
Enter fullscreen mode Exit fullscreen mode

Test Create Resource API

We're going to make POST request to http://localhost:8000/api/developers using Postman. First, we need to set Content-Type and Accept header to application/json to make sure we always receive JSON response from Laravel.

Content-Type and Accept header

Now, we want to insert developer with name Zydhan Linnar Putra and his favourite languange which is C++:

Resource successfully created

Nice! We received response that we want.

Listing Resources

We only need to return all records in database using Developer::all():

public function index() {     return Developer::all(); } 
Enter fullscreen mode Exit fullscreen mode

Test Resources Listing

To test this functionality, we should make GET request to http://localhost:8000/api/developers:

Resource listing Postman

Showing Specific Resource

We only need to return $developer from method parameter:

public function show(Developer $developer) {     return $developer; } 
Enter fullscreen mode Exit fullscreen mode

Test Display Specific Resource

We should make GET request to http://localhost:8000/api/developers/{resource_id}. According to resources listing the resource id is 1, so we need to test http://localhost:8000/api/developers/1:

Specific resource Postman

Updating resources

Like resource creation, we also need to take input from user, and update it to database. But for updating, we don't need to create new Developer instance, we only need to update instance that given in method param:

public function update(Request $request, Developer $developer) {     $developer->name = $request->input('name');     $developer->fav_lang = $request->input('fav_lang');      $developer->save();     return response()->json([ 'message' => 'Resource updated' ]); } 
Enter fullscreen mode Exit fullscreen mode

Test Update Specific Resource

Make a PUT request to http://localhost:8000/api/developers/1 with body structure resource creation, but in this case we want to change his name to Zydhan and make PHP as his favourite language:

Resource update Postman

Then, confirm if the resource is updated:

resource successfully updated Postman

If you look at it, Laravel also automatically update updated_at column.

Delete resource

Eloquent provide delete() method to delete resource from table:

public function destroy(Developer $developer) {     $developer->delete();      return response()->json([ 'message' => 'Resource deleted' ]); } 
Enter fullscreen mode Exit fullscreen mode

Test resource deletion

Method for this request is DELETE and we want to delete developer with name Zydhan which has ID of 1. So we make DELETE request to http://localhost:8000/api/developers/1:

Resource deletion Postman

Now if we access resource listing, we should receive empty array:

Empty array Postman

And if we try to access with resource ID, we got 404 not found:

Not found Postman

Verdict

Laravel bring us ease of API development with eloquent, API controller, migration, etc. There are many room of improvements here, right now we don't have any authentication method so that random user could access our API and do whatever they want. Let me know if you want i make the tutorial to secure your API!

Bonus

API Resources

Typically, you don't want to return all column for listing resources, to achieve it, you can use API Resource to transform eloquent model before returning it.

First, create resource file:

php artisan make:resource DeveloperResource 
Enter fullscreen mode Exit fullscreen mode

We only want to return developer ID and its name, so we only put those columns to resource file:

public function toArray($request) {     return [         'id' => $this->id,         'name' => $this->name     ]; } 
Enter fullscreen mode Exit fullscreen mode

Then, in DeveloperController@index, we should call resource file when returning it.

public function index() {     return DeveloperResource::collection(Developer::all()); } 
Enter fullscreen mode Exit fullscreen mode

DeveloperResource testing

Now, it only return id and name. If we look closer, it also add data, that will be useful if you use pagination. You can read more about API resources here.

Input Validation

What if we don't fill all the input when creating resource? Let's try it:

Test unfilled input

Wow, we got Internal server error. We shouldn't return those error because the problem is from the user. Instead, we should validate data before doing any business logic. First, create request file:

php artisan make:request StoreDeveloperRequest 
Enter fullscreen mode Exit fullscreen mode

Now we have new file app/Http/Requests/StoreDeveloperRequest.php:

<?php  namespace AppHttpRequests;  use IlluminateFoundationHttpFormRequest;  class StoreDeveloperRequest extends FormRequest {     /**      * Determine if the user is authorized to make this request.      *      * @return bool      */     public function authorize()     {         return false;     }      /**      * Get the validation rules that apply to the request.      *      * @return array      */     public function rules()     {         return [             //         ];     } } 
Enter fullscreen mode Exit fullscreen mode

Short explanation:

  • authorize() method is for checking whether it is authorized to execute that request. For now, set it to true to authorize all request.
  • rules() method is for validating input.

Now, we want to make name and fav_lang to be required. Let add it to the rules array:

public function rules() {     return [         'name' => 'required',         'fav_lang' => 'required'     ]; } 
Enter fullscreen mode Exit fullscreen mode

Then, in the controller, substitute Request with StoreDeveloperRequest:

public function store(StoreDeveloperRequest $request) {     $developer = new Developer();     $developer->name = $request->input('name');     $developer->fav_lang = $request->input('fav_lang');      $developer->save();     return response()->json([ 'message' => 'Resource created' ]); } 
Enter fullscreen mode Exit fullscreen mode

Test it again:

Test storing unprocessable content

Now we got proper 422 – Unprocessable Content error and message that we must fill the name and fav_lang column. You can read more about another validation rules beside required here.

Reference

  • API – Wikipedia
  • What is a REST API? – Red Hat
  • Laravel API Tutorial: Building & Testing a RESTful API | Toptal

laravelphptutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.