Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (contact form api | laravel api calling from angular 8 project ) Part 21
In this tutorial, we will create a web api (web service) in Laravel for subscribe block of footer section of our angular project.for create subscribe api in laravel we will make some dependency
1.Create 1 migration and model.so create migration and model . you can use this command
command : php artisan make:model model_name -option
Example : php artisan make:model model/Mypro_subscribeUsers -m
After execute this command you can get two file
- database/migration/create_mypro_subscribe_users_table.php
- app/model/Mypro_subscribeUsers.php
1. open migration file for create database table structure
public function up()
{
Schema::create('mypro_subscribe_users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('email')->nullable();
$table->timestamps();
});
}
Write this code on your laravel project migration file
2.open model file (Mypro_subscribeUsers.php)
<?php
namespace App\model;
use Illuminate\Database\Eloquent\Model;
class Mypro_subscribeUsers extends Model
{
protected $table="mypro_subscribe_users";
protected $primaryKey="id";
protected $fillable=['email'];
}
?>
After model and migration set you can execute one command for create table in database using migration
command : php artisan migrate
After execute this command you can got one table on your datatbase mypro_subscribe_users
Open the FrontApi controller to create a subscribe API
<?php
namespace App\Http\Controllers;
use Validator;
use Illuminate\Http\Request;
use App\model\Mypro_contectQuery;
use App\model\Mypro_subscribeUsers;
class FrontApi extends Controller
{
public function testing(Request $request)
{
print_r($request->all());
}
public function save_contect_query(Request $request)
{
$validator=Validator::make($request->all(),['name'=>'required','email'=>'required','mobile_no'=>'required','message'=>'required']);
if($validator->passes())
{
$obj = new Mypro_contectQuery();
$obj->name = $request->name;
$obj->email = $request->email;
$obj->mobile_no = $request->mobile_no;
$obj->message = $request->message;
$obj->save();
$arr = array('status'=>'true','message'=>'Contect Query Successfully Send');
}
else
{
$arr = array('status'=>'false','message'=>$validator->errors()->all());
}
echo json_encode($arr);
}
public function subscribe_user(Request $request)
{
$validator = Validator::make($request->all(),['email'=>'required']);
if($validator->passes())
{
$check_status=Mypro_subscribeUsers::where('email',$request->email)->get()->toArray();
if($check_status)
{
$arr=array('status'=>'false','message'=>'Email Alredy Exists');
}
else
{
$subscribe = new Mypro_subscribeUsers();
$subscribe->email=$request->email;
$subscribe->save();
$arr=array('status'=>'true','message'=>'Thank You For Subscribe');
}
}
else
{
$arr=array('status'=>'false','message'=>$validator->errors()->all());
}
echo json_encode($arr);
}
}
?>
Write this code on your FrontApi controller
Open api.php file for subscribe api routeing
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*//*Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});*/
Route::post('test','FrontApi@testing');
Route::post('contect_form','FrontApi@save_contect_query');
Route::post('subscribe','FrontApi@subscribe_user');
?>
write this code on api.php file
after use this step you can run this api on postman
Tutorial Topics
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (Angular 8 Project Tutorial Environment Setup ) Part 1
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (How to install bootstrap in angular 8 project)
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (how to install laravel project on localhost ) Part 3
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel | Overview part 4
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (Angular 8 project folder structure) Part 5
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (laravel project folder structure) Part 6
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (Remove public on laravel project URL) Part 7
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (What is component in angular project ) Part 8
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (create user section related component in angular ) Part 9
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (global navigation and footer setup in angular 8 project) Part 10
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (angular 8 project user component routing) Part 11
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (footer section social link angular 8 project ) Part 12
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (home page bootstrap slider setup angular 8 project ) Part 13
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (home page bootstrap slider setup angular 8 project ) Part 14
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (home page service section angular 8 project ) Part 15
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (home page service section angular 8 project ) Part 16
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (laravel project environment setup for web api (web service) ) Part 17
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (model and migration creation and api server side validation in laravel for contect form) Part 18
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (contact form api in laravel ) Part 19
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (contact form api | laravel api calling from angular 8 project ) Part 20
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (contact form api | laravel api calling from angular 8 project ) Part 21
- Angular Projects For Beginners With Laravel | Hotel Management System project with angular 8 and Laravel (contact form api | laravel api calling from angular 8 project ) Part 21