Laravel 5.7 REST API Tutorial Example Steps

Today, We want to share with you Laravel 5.7 REST API Tutorial Example.In this post we will show you Creating APIs in Laravel 5.7 using API resources, hear for Create restful APIs using Laravel 5.7 with resourceful routes example we will give you demo and example for implement.In this post, we will learn about Create REST API in Laravel 5.7 with authentication using Passport with an example.

Laravel 5.7 REST API Tutorial Example

There are the Following The simple About Laravel 5.7 REST API Tutorial Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop PHP Laravel 5.7 – Rest API with Passport Tutorial, so the Laravel 5.7 – Create REST API with authentication using Passport Tutorial is following below.

There is All the API Routes URL using Laravel 5.7

  • Student Login: API:GET, URL:http://student-management/oauth/token
  • Student Signup: API:GET, URL:http://student-management/api/signup
  • Student List: API:GET, URL:http://student-management/api/students
  • Student Create: API:POST, URL:http://student-management/api/students
  • Student Show: API:GET, URL:http://student-management/api/students/{id}
  • Student Update: API:PUT, URL:http://student-management/api/students/{id}
  • Student Delete: API:DELETE, URL:http://student-management/api/students/{id}

Step 1: Install Laravel 5.7 Project

Step by step from Full Example scratch in fresh Laravel 5.7 application

composer create-project --prefer-dist laravel/laravel atmiya25

Step 2: Setup Passport


// Setup passport via the Composer
composer require laravel/passport

//default migration
php artisan migrate

//Setup passport using command in Laravel 5.7
php artisan passport:install

Step 3: Laravel 5.7 Passport Settings

app/Member.php


app/Providers/AuthServiceProvider.php

 'App\Policies\ModelPolicy',
    ];

    public function boot()
    {
        $this->signupPolicies();
        Passport::routes();
    }
}

config/auth.php

 [
        'web' => [
            'driver' => 'session',
            'provider' => 'members',
        ],
        'api' => [
            'driver' => 'passport',
            'provider' => 'members',
        ],
    ],
    .....
	...............
]

Step 4: Laravel 5.7 Add Student Table and Model

php artisan make:migration create_students_table

database/migrations

increments('id');
            $table->string('name');
            $table->text('detail');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('students');
    }
}

create Laravel 5.7 migration

php artisan migrate

app/Student.php


Step 5: Laravel 5.7 API Routes

routes/api.php

group( function () {
	Route::resource('students', 'API\StudentController');
});

Step 6: Make a Laravel 5.7 Controller Files

app/Http/Controllers/API/CustomController.php

 true,
            'data'    => $result,
            'message' => $message,
        ];


        return response()->json($response, 200);
    }

    public function apiResponseError($error, $msgError = [], $code = 404)
    {
    	$response = [
            'success' => false,
            'message' => $error,
        ];


        if(!empty($msgError)){
            $response['data'] = $msgError;
        }


        return response()->json($response, $code);
    }
}

app/Http/Controllers/API/StudentController.php

apiResponseSuccess($students->toArray(), 'Students retrieved successfully.');
    }

    public function store(Request $request)
    {
        $input = $request->all();


        $validator = Validator::make($input, [
            'name' => 'required',
            'detail' => 'required'
        ]);

        if($validator->fails()){
            return $this->apiResponseError('Validation Error.', $validator->errors());       
        }
        $student = Student::create($input);
        return $this->apiResponseSuccess($student->toArray(), 'Student created successfully.');
    }

    public function show($id)
    {
        $student = Student::find($id);
        if (is_null($student)) {
            return $this->apiResponseError('Student not found.');
        }
        return $this->apiResponseSuccess($student->toArray(), 'Student retrieved successfully.');
    }
    public function update(Request $request, Student $student)
    {
        $input = $request->all();
        $validator = Validator::make($input, [
            'name' => 'required',
            'detail' => 'required'
        ]);

        if($validator->fails()){
            return $this->apiResponseError('Validation Error.', $validator->errors());       
        }

        $student->name = $input['name'];
        $student->detail = $input['detail'];
        $student->save();
        return $this->apiResponseSuccess($student->toArray(), 'Student updated successfully.');
    }

    public function destroy(Student $student)
    {
        $student->delete();
        return $this->apiResponseSuccess($student->toArray(), 'Student deleted successfully.');
    }
}

app/Http/Controllers/API/SignupController.php

all(), [
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required',
            'c_password' => 'required|same:password',
        ]);


        if($validator->fails()){
            return $this->apiResponseError('Validation Error.', $validator->errors());       
        }

        $input = $request->all();
        $input['password'] = bcrypt($input['password']);
        $member = Member::create($input);
        $success['token'] =  $member->createToken('MyApp')->accessToken;
        $success['name'] =  $member->name;


        return $this->apiResponseSuccess($success, 'Member signup successfully.');
    }
}

run Laravel 5.7 restful api Project

php artisan serve

Include Laravel 5.7 Must in Headers part

'headers' => [
    'Accept' => 'application/json',
    'Authorization' => 'Bearer '.$accessToken,
]
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel 5.7 REST API Tutorial Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment