Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jpalala/1bd14f6ae9f87d0c5933e3ec3b8dc0ea to your computer and use it in GitHub Desktop.
Save jpalala/1bd14f6ae9f87d0c5933e3ec3b8dc0ea to your computer and use it in GitHub Desktop.

Revisions

  1. @deepak-cotocus deepak-cotocus revised this gist Aug 22, 2020. No changes.
  2. @deepak-cotocus deepak-cotocus revised this gist Aug 21, 2020. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions laravel-api-resources.md
    Original file line number Diff line number Diff line change
    @@ -67,9 +67,7 @@ Route::get('/json', function () {
    + Here my Application is **`'demo-app'`** and its virtual host url is **` http://demo-app/ `**.
    So i will open browser and hit: **`http://demo-app/json`** and see tha magic of **LARAVEL RESOURCES**

    ```
    <img width="960" alt="resources-json" src="https://user-images.githubusercontent.com/62638864/90886462-10a25c00-e3d0-11ea-8b18-76ad948bfdbc.png">
    ---

    ## My basic recommendation for learning : [Eloquent: API Resources](https://laravel.com/docs/7.x/eloquent-resources)

  3. @deepak-cotocus deepak-cotocus revised this gist Aug 21, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions laravel-api-resources.md
    Original file line number Diff line number Diff line change
    @@ -68,10 +68,10 @@ Route::get('/json', function () {
    So i will open browser and hit: **`http://demo-app/json`** and see tha magic of **LARAVEL RESOURCES**

    ```
    <img width="960" alt="resources-json" src="https://user-images.githubusercontent.com/62638864/90886462-10a25c00-e3d0-11ea-8b18-76ad948bfdbc.png">
    ---
    ## My basic recommendation for learning : [API Authentication (Passport)](https://laravel.com/docs/5.5/passport)
    ## My basic recommendation for learning : [Eloquent: API Resources](https://laravel.com/docs/7.x/eloquent-resources)
    ## Thanks ##
  4. @deepak-cotocus deepak-cotocus revised this gist Aug 21, 2020. 1 changed file with 31 additions and 63 deletions.
    94 changes: 31 additions & 63 deletions laravel-api-resources.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    ### Introduction: ###
    #### What is API Resources in Laravel####
    ### Introduction:
    #### What is API Resources in Laravel

    > __LARAVEL's__ resource classes allow you to expressively and easily transform your models and model collections into JSON.Basically you can generate a nice json formatted data right from Eloquent.
    Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.
    @@ -10,94 +10,62 @@ Let’s learn how to generate api resources.Consider you have a user table in yo
    + By default, resources will be placed in the `app/Http/Resources` directory of your application. Resources extend the `Illuminate\Http\Resources\Json\JsonResource` class:

    **`php artisan make:resource User`**

    <img width="960" alt="resources" src="https://user-images.githubusercontent.com/62638864/90884712-e059be00-e3cd-11ea-970a-21cb8dacb3c0.png">

    #### STEP 2: Concept Overview
    Before diving into all of the options available to you when writing resources, let's first take a high-level look at how resources are used within Laravel. A resource class represents a single model that needs to be transformed into a JSON structure. For example, here is a simple User resource class:

    ```php


    ```

    #### STEP 3:
    command to create the encryption keys needed to generate secure access tokens. In addition, the command will create "personal access" and "password grant" clients which will be used to generate access tokens:
    **`php artisan passport:install`**

    #### STEP 4:
    After running this command, add the `Laravel\Passport\HasApiTokens` trait to your `App\User` model. This trait will provide a few helper methods to your model which allow you to inspect the authenticated user's token and scopes:
    +Change the content of above file as shown below.

    ```php

    <?php

    namespace App;
    namespace App\Http\Resources;

    use Laravel\Passport\HasApiTokens;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Http\Resources\Json\Resource;
    use Illuminate\Support\Facades\Log;

    class User extends Authenticatable
    class User extends Resource
    {
    use HasApiTokens, Notifiable;
    /**
    * Transform the resource into an array.
    *
    * @param \Illuminate\Http\Request $request
    * @return array
    */
    public function toArray($request)
    {
    return parent::toArray($request);
    }
    }

    ```

    #### STEP 5:
    Next, you should call the `Passport::routes` method within the `boot` method of your `AuthServiceProvider`. This method will register the routes necessary to issue access tokens and revoke access tokens, clients, and personal access tokens:

    #### STEP 3: Add below Changes in `web.php` file
    ```php

    <?php

    namespace App\Providers;
    use App\User;
    use App\Http\Resources\User as UserResource;

    use Laravel\Passport\Passport;
    use Illuminate\Support\Facades\Gate;
    use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

    class AuthServiceProvider extends ServiceProvider
    {
    /**
    * The policy mappings for the application.
    *
    * @var array
    */
    protected $policies = [
    'App\Model' => 'App\Policies\ModelPolicy',
    ];
    Route::get('/', function () {
    return view('welcome');
    });

    /**
    * Register any authentication / authorization services.
    *
    * @return void
    */
    public function boot()
    {
    $this->registerPolicies();
    Route::get('/json', function () {

    Passport::routes();
    }
    }
    $users = User::first();
    return new UserResource($users);
    });

    ```
    #### STEP 6:
    Finally, in your `config/auth.php` configuration file, you should set the `driver` option of the `api` authentication guard to `passport`. This will instruct your application to use Passport's `TokenGuard` when authenticating incoming API requests:

    ```php

    'guards' => [
    'web' => [
    'driver' => 'session',
    'provider' => 'users',
    ],

    'api' => [
    'driver' => 'passport',
    'provider' => 'users',
    ],
    ],
    #### STEP 4: All set to go
    + Here my Application is **`'demo-app'`** and its virtual host url is **` http://demo-app/ `**.
    So i will open browser and hit: **`http://demo-app/json`** and see tha magic of **LARAVEL RESOURCES**

    ```
  5. @deepak-cotocus deepak-cotocus revised this gist Aug 21, 2020. 1 changed file with 14 additions and 8 deletions.
    22 changes: 14 additions & 8 deletions laravel-api-resources.md
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,24 @@
    ###Introduction: ###
    ####What is API Resources in Laravel####
    ### Introduction: ###
    #### What is API Resources in Laravel####

    > __LARAVEL's__ resource classes allow you to expressively and easily transform your models and model collections into JSON.Basically you can generate a nice json formatted data right from Eloquent.
    Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.
    Let’s learn how to generate api resources.Consider you have a user table in your database and you want to generate api resources for your User model.Run following command on your terminal window while being on laravel project root directory:

    #### STEP 1: Generating Resources
    + Run this Command to generate a resource class.
    + By default, resources will be placed in the `app/Http/Resources` directory of your application. Resources extend the `Illuminate\Http\Resources\Json\JsonResource` class:

    **`php artisan make:resource User`**

    #### STEP 1: Installation
    install Passport via the Composer package manager:
    **`composer require laravel/passport=~4.0`**

    #### STEP 2: Migrate yore Database
    The Passport service provider registers its own database migration directory with the framework, so you should migrate your database after registering the provider. The Passport migrations will create the tables your application needs to store clients and access tokens:
    **`php artisan migrate`**
    #### STEP 2: Concept Overview
    Before diving into all of the options available to you when writing resources, let's first take a high-level look at how resources are used within Laravel. A resource class represents a single model that needs to be transformed into a JSON structure. For example, here is a simple User resource class:

    ```php


    ```

    #### STEP 3:
    command to create the encryption keys needed to generate secure access tokens. In addition, the command will create "personal access" and "password grant" clients which will be used to generate access tokens:
  6. @deepak-cotocus deepak-cotocus created this gist Aug 21, 2020.
    103 changes: 103 additions & 0 deletions laravel-api-resources.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    ###Introduction: ###
    ####What is API Resources in Laravel####

    > __LARAVEL's__ resource classes allow you to expressively and easily transform your models and model collections into JSON.Basically you can generate a nice json formatted data right from Eloquent.
    Let’s learn how to generate api resources.Consider you have a user table in your database and you want to generate api resources for your User model.Run following command on your terminal window while being on laravel project root directory:



    #### STEP 1: Installation
    install Passport via the Composer package manager:
    **`composer require laravel/passport=~4.0`**

    #### STEP 2: Migrate yore Database
    The Passport service provider registers its own database migration directory with the framework, so you should migrate your database after registering the provider. The Passport migrations will create the tables your application needs to store clients and access tokens:
    **`php artisan migrate`**

    #### STEP 3:
    command to create the encryption keys needed to generate secure access tokens. In addition, the command will create "personal access" and "password grant" clients which will be used to generate access tokens:
    **`php artisan passport:install`**

    #### STEP 4:
    After running this command, add the `Laravel\Passport\HasApiTokens` trait to your `App\User` model. This trait will provide a few helper methods to your model which allow you to inspect the authenticated user's token and scopes:

    ```php

    <?php

    namespace App;

    use Laravel\Passport\HasApiTokens;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;

    class User extends Authenticatable
    {
    use HasApiTokens, Notifiable;
    }

    ```

    #### STEP 5:
    Next, you should call the `Passport::routes` method within the `boot` method of your `AuthServiceProvider`. This method will register the routes necessary to issue access tokens and revoke access tokens, clients, and personal access tokens:

    ```php

    <?php

    namespace App\Providers;

    use Laravel\Passport\Passport;
    use Illuminate\Support\Facades\Gate;
    use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

    class AuthServiceProvider extends ServiceProvider
    {
    /**
    * The policy mappings for the application.
    *
    * @var array
    */
    protected $policies = [
    'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
    * Register any authentication / authorization services.
    *
    * @return void
    */
    public function boot()
    {
    $this->registerPolicies();

    Passport::routes();
    }
    }

    ```
    #### STEP 6:
    Finally, in your `config/auth.php` configuration file, you should set the `driver` option of the `api` authentication guard to `passport`. This will instruct your application to use Passport's `TokenGuard` when authenticating incoming API requests:

    ```php

    'guards' => [
    'web' => [
    'driver' => 'session',
    'provider' => 'users',
    ],

    'api' => [
    'driver' => 'passport',
    'provider' => 'users',
    ],
    ],

    ```

    ---

    ## My basic recommendation for learning : [API Authentication (Passport)](https://laravel.com/docs/5.5/passport)


    ## Thanks ##