Posts

Showing posts from August, 2022

Resources API

Image
Resources data in database using API in LARAVEL Create Method of  MemberController   in Controller: Command: php artisan make:controller MemberController --resource Create Controller: <?php namespace App \ Http \ Controllers ; use App \ Models \ ClientModel ; use Illuminate \ Http \ Request ; class MemberController extends Controller {     /**      * Display a listing of the resource.      *      * @return \ Illuminate \ Http \ Response      */     public function index ()     {         return ClientModel :: all() ;   // all data fetch form DB     }     /**      * Show the form for creating a new resource.      *      * @return \ Illuminate \ Http \ Response      */     public function create ()     {         //     } ...

Validation API

Image
Simplest function Validation API Create Method of Validation  in Controller:  function validator ( Request $req )     {         // Simplest function Validation API in Postman         $rules = array (             "name" => "required|min:3|max:15"         );         $validator = Validator :: make($req -> all() , $rules) ;         if ($validator -> fails()){             return Response() -> json( $validator -> errors() , 404 ) ;         }         else {             // Post data in the Database from Postman         $clientmodel = new ClientModel ;         $clientmodel -> name = $req -> name ;         $clientmodel -> email = $req -> email ;     ...

Delete API

Image
  Delete Data using API in LARAVEL Create Method for Delete Data in Controller: function delete ( $id )     {         $clientmodel = ClientModel :: find($id) ;         $result = $clientmodel -> delete() ;         if ($result)         {             return [ "return" => "Data Deleted Successfully.." ];         }         else         {             return [ "return" => "Data Deleted Failed.." ];         }     } Now Create Delete Route in api: Route :: delete( "deletedata/{id}" , [ DBController :: class , 'delete' ] ) ; O/P

API Search Data

Image
   Search data in database using API in LARAVEL Create Method of Search Data in Controller:  function search ( $name )     {         return ClientModel :: where( "name" , "like" , "%" . $name . "%" ) -> get() ;     } Now Create Search Route in api: // ("name", "like", "%" . $name . "%") means if one latter search then also Details show Route :: get( "searchdata/{name}" , [ DBController :: class , 'search' ] ) ; O/P ( "name" , "like" , "%" . $name . "%" ) -> get() ( "name" , $name ) -> get()

PUT API

Image
 Save data in the database using the PUT Method. First of all, Replace PUT after then same Now Create a Post Method in Controller.   // Put API     //Save data in database using Put Method     function update ( Request $req )     {         $clientmodel = ClientModel :: find($req -> id) ;         $clientmodel -> name = $req -> name ;         $clientmodel -> email = $req -> email ;         $result = $clientmodel -> save() ;         if ($result)         {             return [ "Result" => "Data Updated Successfully" ];         }         else         {             return [ "Result" => "Data Update Failed" ];         }     } Note: Write in ClientModel ...

POST API

Image
Save data in the database using the POST Method. First of all Change the  Now Create a Post Method in Controller. function post ( Request $req )     {         $clientmodel = new ClientModel ;         $clientmodel -> name = $req -> name ;         $clientmodel -> email = $req -> email ;         $result = $clientmodel -> save() ;         if ($result)         {             return [ "Result" => "Data Post Successfully" ];         }         else         {             return [ "Result" => "Data Post Failed" ];         }     } Note: Write in ClientModel  public $timestamps = false ; Now create the Route Route :: post( "adds" , [ DBController :: class , 'post' ] ) ; Output:

Get API with Parameter

 How to Get API with Parameter.? Go to the Controller and create the Method. function listparam ( $id = null )     {         return ClientModel :: find($id) ;     }   Now also change the API Routes Route :: get( "lists/{id}" , [ DBController :: class , 'listparam' ] ) ; Now OutPut Result

get data with API

Image
 How to get data with API.? 1. Step Go Terminal php artisan make:migration create_abc_table     Press Enter Create Table 2. Step  go Terminal  php artisan migrate 3. Step  Check Database 4. Step Let's start get data with API Create the Controller go Terminal php artisan make:controller  DBController     // Get data with API form DB  function list ()     {         return ClientModel :: all() ;     } 5. Step Create the Model go Terminal php artisan make:model  ClientModel class ClientModel extends Model {     use HasFactory ;     protected $table = 'clientmodel' ;     protected $fillable = [ 'name' , 'email' , 'address' ]; } 6. Step go routes and create the route Route :: get( "lists" , [ DBController :: class , 'list' ] ) ; Output:

Connect DB with Laravel

Image
 How to Connect Database with Laravel.?

Get Data From Api

Image
  Make first the simplest API. 1. Create Controller Command   php artisan make:controller ApiController 2. Now Create the Method. function getdata ()     {         return [ "name" => "Aqib" , "email" => "aqib123@gamil.com" , "address" => "Pakistan" ];     } 3.Go the routes Select Api.php Generate the Route for controller Route :: get( "getData" , [ ApiController :: class , 'getdata' ] ) ; 4. Now go the Postman Insert the server link http://127.0.0.1:8000/api/getData OutPut:

Create Laravel API App

Image
 1. Step Select the Folder and write the Project Name, Press Enter 2. Step Now Create Laravel App 3. Step  Check the Folder Now Create the Completed App  4.And Then  Download Postman Extension                                                            -------------------Thank you-----------------

Laravel API Intro

Image
  What is Laravel API? API Stands for Application Programming Interface. Laravel. Laravel is  a PHP framework developed with PHP developer productivity in mind . The framework also aims to evolve with the web and has already incorporated several new features and ideas in the web development world, such as job queues, API authentication out of the box, real-time communication, and much more. Why do we use API in Laravel.? It is used for transferring data between two technology. Some technology can not connect to the DB directly, they need a medium to communicate. E.g  Angular, Rect, View, ios & Android.