Complete Laravel installation and API guide

How to install Laravel using composer

composer create-project laravel/laravel example-app

cd example-app

php artisan serve

Create View in Laravel

<!-- View stored in resources/views/home.blade.php -->

<html>
    <body>
        <h1>My Home Page {{ $name }}</h1>
    </body>
</html>


<!-- Route stored in routes/web.php -->


Route::get('/', function () {
    return view('greeting', ['name' => 'James']);
});

Create model, controller and view in Laravel

Step 1: Create model and controller

php artisan make:controller HomeController --resource --model=Home

Step 2: Create View in Laravel



<!-- View stored in resources/views/home.blade.php -->

<html>
    <body>
        <h1>My Home Page {{ $name }}</h1>
    </body>
</html>

Step 3: call controller using route and display view




<!-- Route stored in routes/web.php -->

Route::get('/home', 'App\Http\Controllers\HomeController@index');

or 

Route::get('/home', 'HomeController@index');

Note: changing the RouteServiceProvider.php file in App/Providers/ path by uncommenting the code.

protected $namespace = ‘App\Http\Controllers’;

Create and Rest Api in Laravel

php artisan make:controller ApiController

You will find a new file named ApiController.php in the app\http\controllers directory. Next, we can add the following methods:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ApiController extends Controller
{
    public function sendResponse($result, $message)
  {
    $response = [
          'success' => true,
          'data'    => $result,
          'message' => $message,
      ];
   return response()->json($response, 200);
  }

  /**
     * return error response.
     *
     * @return \Illuminate\Http\Response
     */
    public function sendError($error, $errorMessages = [], $code = 404)
    {
    	$response = [
            'success' => false,
            'message' => $error,
        ];


        if(!empty($errorMessages)){
            $response['data'] = $errorMessages;
        }


        return response()->json($response, $code);
    }


//
    public function getList() {

      /*  $test = Test::get()->toJson(JSON_PRETTY_PRINT);
        return response($tests, 200);*/

      /*  return response()->json([
            "message" => "student record created"
        ], 201);*/

$data= array();     
          $data = DB::connection('default')->table('users')->where('id',$id)->get();
        return $this->sendResponse($data, 'Successfully');
        
      }
}

Proceed to the routes directory and open the api.php file and create the endpoints that will reference the methods created earlier in the ApiController

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

<!-- Route stored in routes/api.php -->

Route::get('getList', 'App\Http\Controllers\ApiController@getList');

or

Route::get('getList', 'ApiController@getList');

Test Api using Postman

Useful command for Laravel

composer dumpautoload

php artisan cache:clear

php artisan config:clear

php artisan view:clear

command php artisan list

 make:channel         Create a new channel class
  make:command         Create a new Artisan command
  make:controller      Create a new controller class
  make:event           Create a new event class
  make:exception       Create a new custom exception class
  make:factory         Create a new model factory
  make:job             Create a new job class
  make:listener        Create a new event listener class
  make:mail            Create a new email class
  make:middleware      Create a new middleware class
  make:migration       Create a new migration file
  make:model           Create a new Eloquent model class
  make:notification    Create a new notification class
  make:observer        Create a new observer class
  make:policy          Create a new policy class
  make:provider        Create a new service provider class
  make:request         Create a new form request class
  make:resource        Create a new resource
  make:rule            Create a new validation rule
  make:seeder          Create a new seeder class
  make:test            Create a new test class

Leave a Reply