Laravel is a PHP framework developed with PHP developer for fast development .Insert Data using REST API in Laravel.
RESTful APIs
First, we need to understand what exactly is considered a RESTful API. REST stands for REpresentational State Transfer and is an architectural style for network communication between applications, which relies on a stateless protocol (usually HTTP) for interaction.
Set routing first for signup process .
I am not showing here full configuration of REST API . So need to do proper configuration for REST API in Laravel.
routes.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController ;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/register',[AuthController ::class,'register']);
Now Below code is for the Signup process. Our Model name is, the user so there should be one table with the names of users in the database. Because I am using the Eloquent feature of Laravel.
AuthController.php
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class AuthController extends Controller
{
public function register(Request $request)
{
$name = $request->name;
$email = $request->email;
$password = $request->password;
// Check if field is empty
if (empty($name) or empty($email) or empty($password)) {
return response()->json(['status' => 'error', 'message' => 'You must fill all the fields']);
}
// Check if email is valid
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return response()->json(['status' => 'error', 'message' => 'You must enter a valid email']);
}
// Check if password is greater than 5 character
if (strlen($password) < 6) {
return response()->json(['status' => 'error', 'message' => 'Password should be min 6 character']);
}
// Check if user already exist
if (User::where('email', '=', $email)->exists()) {
return response()->json(['status' => 'error', 'message' => 'User already exists with this email']);
}
// Create new user
try {
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = app('hash')->make($request->password);
if ($user->save()) {
return $this->login($request);
}
} catch (\Exception $e) {
return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
}
}
}?>
Now you can Test this controller in postman
http://localhost:8000/api/register
Subscribe to My Programming YouTube Channel Before Downloading the code :
Read our more Tutorials on React and Laravel
Signup using REST API in Laravel
June 25, 2021UncategorizedComments: 0
Laravel is a PHP framework developed with PHP developer for fast development .Insert Data using REST API in Laravel. RESTful APIs First, we need to understand what exactly is considered a RESTful API. REST stands for REpresentational State Transfer and is an architectural style for network communication between applications, which relies on a stateless protocol (usually HTTP) for interaction. …
Email Validation in ReactJS
June 25, 2021REACT JSComments: 0
Email validation is a very important step in any web application. Email Validation in ReactJS. Email Validation can be achieved using the validator package or module in ReactJS.The below example will show how You can validate your Email input box using some very secure validation Technique. React Application And Installing Module: We can install react app using …
Send Email using PHP
June 24, 2021PHPComments: 0
Sending email messages are very common features a web application .Send Email using PHP You can use the PHP mail() function for creating and sending email messages to one or more recipients dynamically from your PHP application either in a plain-text form or formatted HTML. Learn more about PHP Mail Function https://www.php.net/manual/en/function.mail.php The Basic syntex of …
Routing in Node.js
June 20, 2021NODE JSComments: 0
What is Routing?Routing defines the way in which the client or front-end requests are handled by the application endpoints ( Server SIde ).Routing in Node.js Download Node from here https://nodejs.org/en/ Implementation of routing in Node.js: There are two ways to implement routing in node.js which are listed below: Framework No Framework But We will use Normal …