Skip to content

Instantly share code, notes, and snippets.

@deepak-cotocus
Last active August 22, 2020 08:06
Show Gist options
  • Save deepak-cotocus/817bea0092866d81df731273465b2518 to your computer and use it in GitHub Desktop.
Save deepak-cotocus/817bea0092866d81df731273465b2518 to your computer and use it in GitHub Desktop.

Revisions

  1. deepak-cotocus revised this gist Aug 22, 2020. No changes.
  2. deepak-cotocus revised this gist Aug 22, 2020. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions laravel-api-resources2.md
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,9 @@

    >In Our previous tutorial we have seen how to generate json data in which we just did a Direct Map of our User database table.
    **Now, We are going to play arrout Jason data as per our requirement.Every Time Requirements will be diffrent Right......
    So we are gona generate diffrent numbers of data in many ways...
    **Now, We are going to play arround Json data as per our requirement.
    Every Time Requirements will be diffrent Right......
    So we are gona generate different numbers of data in many ways...
    Just go for it Guys...!**

    + If i don't need `id`,`created_at` and `updated_at` field in my Json Object.
  3. deepak-cotocus revised this gist Aug 22, 2020. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions laravel-api-resources2.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    So we are gona generate diffrent numbers of data in many ways...
    Just go for it Guys...!**

    + If i dont need id,created_at and updated_at
    + If i don't need `id`,`created_at` and `updated_at` field in my Json Object.
    what would i do then...

    + Lets see where i will make Changes in code.
    @@ -37,11 +37,10 @@ class User extends Resource
    public function toArray($request)
    {
    return [
    'id' => $this->id,

    'name' => $this->name,
    'email' => $this->email,
    'created_at' => $this->created_at,
    'updated_at' => $this->updated_at,

    ];
    }
    }
    @@ -75,7 +74,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-customized1" src="https://user-images.githubusercontent.com/62638864/90950029-860f3a80-e46b-11ea-8bea-2e250e2d342a.png">

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

  4. deepak-cotocus revised this gist Aug 22, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions laravel-api-resources2.md
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,10 @@
    So we are gona generate diffrent numbers of data in many ways...
    Just go for it Guys...!**

    +If i dont need id,created_at and updated_at
    + If i dont need id,created_at and updated_at
    what would i do then...

    +Lets see where i will make Changes in code.
    + Lets see where i will make Changes in code.
    NOte: Make sure you have Resourec folder with User class under `<Your-app>\app\http\Resources`

    #### STEP 1: Concept Overview
    @@ -49,7 +49,7 @@ class User extends Resource
    ```

    #### STEP 2: Add below Changes in `web.php` file
    +If you already have below lines of code in your web.php, then no need of any change.
    + If you already have below lines of code in your web.php, then no need of any change.

    ```php

  5. deepak-cotocus revised this gist Aug 22, 2020. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions laravel-api-resources2.md
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,14 @@

    >In Our previous tutorial we have seen how to generate json data in which we just did a Direct Map of our User database table.
    --Now, We are going to play arrout Jason data as per our requirement.Every Time Requirements will be diffrent Right......
    So we are gona generate diffrent numbers of data in many ways...Just go for it Guys...!
    **Now, We are going to play arrout Jason data as per our requirement.Every Time Requirements will be diffrent Right......
    So we are gona generate diffrent numbers of data in many ways...
    Just go for it Guys...!**

    If i dont need id,created_at and updated_at
    +If i dont need id,created_at and updated_at
    what would i do then...

    Lets see where i will make Changes in code.
    +Lets see where i will make Changes in code.
    NOte: Make sure you have Resourec folder with User class under `<Your-app>\app\http\Resources`

    #### STEP 1: Concept Overview
  6. deepak-cotocus created this gist Aug 22, 2020.
    82 changes: 82 additions & 0 deletions laravel-api-resources2.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    ### Introduction:
    #### How to generate customized Json data using resource in Laravel?

    >In Our previous tutorial we have seen how to generate json data in which we just did a Direct Map of our User database table.
    --Now, We are going to play arrout Jason data as per our requirement.Every Time Requirements will be diffrent Right......
    So we are gona generate diffrent numbers of data in many ways...Just go for it Guys...!

    If i dont need id,created_at and updated_at
    what would i do then...

    Lets see where i will make Changes in code.
    NOte: Make sure you have Resourec folder with User class under `<Your-app>\app\http\Resources`

    #### STEP 1: Concept Overview

    +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 [
    'id' => $this->id,
    'name' => $this->name,
    'email' => $this->email,
    'created_at' => $this->created_at,
    'updated_at' => $this->updated_at,
    ];
    }
    }

    ```

    #### STEP 2: Add below Changes in `web.php` file
    +If you already have below lines of code in your web.php, then no need of any change.

    ```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 3: 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**



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


    ## Thanks ##