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

  1. database/migration/create_mypro_subscribe_users_table.php
  2. 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