-
-
Save jpalala/1bd14f6ae9f87d0c5933e3ec3b8dc0ea to your computer and use it in GitHub Desktop.
Revisions
-
deepak-cotocus revised this gist
Aug 22, 2020 . No changes.There are no files selected for viewing
-
deepak-cotocus revised this gist
Aug 21, 2020 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) -
deepak-cotocus revised this gist
Aug 21, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 : [Eloquent: API Resources](https://laravel.com/docs/7.x/eloquent-resources) ## Thanks ## -
deepak-cotocus revised this gist
Aug 21, 2020 . 1 changed file with 31 additions and 63 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ ### 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: +Change the content of above file as shown below. ```php <?php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\Resource; use Illuminate\Support\Facades\Log; class User extends Resource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array */ public function toArray($request) { return parent::toArray($request); } } ``` #### STEP 3: Add below Changes in `web.php` file ```php <?php use App\User; use App\Http\Resources\User as UserResource; Route::get('/', function () { return view('welcome'); }); Route::get('/json', function () { $users = User::first(); return new UserResource($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** ``` -
deepak-cotocus revised this gist
Aug 21, 2020 . 1 changed file with 14 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,24 @@ ### 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 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: -
deepak-cotocus created this gist
Aug 21, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ##