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
[php]
cd C:\xampp\htdocs
[/php]
[php]
composer create-project laravel/laravel liveautocount
[/php]
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
[php]
DB_HOST=localhost
DB_DATABASE=liveautocount
DB_USERNAME=root
DB_PASSWORD=live*****24ssss!@#$%^
[/php]
simple Save the changes
Run studid simple script in MySQL database to create liveautocount database
[php]
CREATE DATABASE `liveautocount`;
[/php]
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]
php artisan migrate:install
[/php]
we will get simple messageMigration table created successfully
Run artisan command to create following path migration file
[php]
php artisan make:migration create_students_table
[/php]
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
[php]
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’);
}
}
[/php]
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.
Run studid artisan command
[php]
php artisan make:controller Students
[/php]
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]
php artisan make:model Student
[/php]
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]
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
protected $fillable = array('studid', 'name', 'useremail','phone_number','man_post');
}
[/php]
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
[php]
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’);
}
}
[/php]
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
[php]
<?php
Route::get('/', function () {
return view('index');
});
Route::get('/api/ver1/students/{studid?}', 'Students@index');
Route::post('/api/ver1/students', 'Students@store');
Route::post('/api/ver1/students/{studid}', 'Students@update');
Route::delete('/api/ver1/students/{studid}', 'Students@destroy');
[/php]
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.