Laravel Create REST API Step By Step with Authentication

Laravel Create REST API Step By Step with Authentication

In this Post We Will Explain About is Laravel Create REST API Step By Step with Authentication With Example and Demo.Welcome on infinityknow.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Laravel 5 AngularJS backend REST APIExample

In this post we will show you Best way to implement API Authentication – Laravel, hear for API Token Authentication in Laravel 5.2 and 5.3with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Step 1: Create new setup Laravel 5 Application

cd C:\xampp\htdocs

composer create-project laravel/laravel liveautocount

The above sour new simplece code creates a new simple project in htdocs xampp or any server named liveautocount

Step 2: Database migrations

your new simple apps path and changes config file .env file in the Laravel project root

READ :  C# Boxing & Unboxing Tutorial with Examples

DB_HOST=localhost
DB_DATABASE=liveautocount
DB_USERNAME=root
DB_PASSWORD=live*****[email protected]#$%^

simple Save the changes

Run studid simple script in MySQL database to create liveautocount database

CREATE DATABASE `liveautocount`;

We will use the PHP artisan command to create a simple table mysql migration file that will create a new table for student records.

php artisan migrate:install

we will get simple messageMigration table created successfully

Run artisan command to create following path migration file

php artisan make:migration create_students_table

we will get studid messageCreated migration: 2018_08_05_088758 _create_students_table

your new simple apps path and changes your new simple apps path and changes /database/migrations/20180803088758 createstudentstableModify the contents to studid

increments('studid');
            $table->string('name')->unique();
            $table->string('useremail')->unique();
            $table->string('phone_number');
            $table->string('man_post');
            $table->timestamps();
        });
    }

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

}

Save the changes.Run studid artisan command cmd to run simple the migration

php artisan migrate

Step 3: Simple REST API

and then now create a laravel controller for our new simple new REST API.

READ :  AngularJS AutoComplete Search Dynamic Data using Web API

Run studid artisan command

php artisan make:controller Students

we will get studid message Controller created successfully.and then now create an simple model level Eloquent ORM model for our new simple REST API

php artisan make:model Student

we will get studid messageModel created successfully.
and then now add a fillable array to our new simple model your new simple apps path and changes Student.php controller in /app/Student.php

Modify the code to studid

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
   protected $fillable = array('studid', 'name', 'useremail','phone_number','man_post');
}

and then now modify the controller code

your new simple apps path and changes Students.php in /app/Http/Controllers/Students.php

Update the source code to studid

get();
        } else {
            return $this->show($studid);
        }
    }

    public function store(Request $request) {
        $student = new Student;

        $student->name = $request->input('name');
        $student->useremail = $request->input('useremail');
        $student->phone_number = $request->input('phone_number');
        $student->man_post = $request->input('man_post');
        $student->save();

        return 'Student record successfully created with studid ' . $student->studid;
    }

    public function show($studid) {
        return Student::find($studid);
    }

    public function update(Request $request, $studid) {
        $student = Student::find($studid);

        $student->name = $request->input('name');
        $student->useremail = $request->input('useremail');
        $student->phone_number = $request->input('phone_number');
        $student->man_post = $request->input('man_post');
        $student->save();

        return "Sucess updating user #" . $student->studid;
    }

    public function destroy(Request $request) {
        $student = Student::find($request->input('studid'));

        $student->delete();

        return "Student record successfully deleted #" . $request->input('studid');
    }
} 

We now need to here path define the routes for our new simple SLIM REST API your new simple apps path and changes
/app/Http/routes.php
Modify the simple PHP code to studid

READ :  Vuejs Examples - Vuejs examples for beginners - application Vuejs 2

<?php
Route::get('/', function () {
    return view('index');
});

Route::get('/api/ver1/students/{studid?}', '[email protected]');
Route::post('/api/ver1/students', '[email protected]');
Route::post('/api/ver1/students/{studid}', '[email protected]');
Route::delete('/api/ver1/students/{studid}', '[email protected]');

Example

I hope you have Got What is Laravel 5 Basic RESTFull API Authentication And how it works.I would Like to have FeadBack From My Blog(infinityknow.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(infinityknow.com) Are Most Always Welcome.

Add a Comment

Your email address will not be published. Required fields are marked *