1

I am trying to rename my data wrapper for the resource I am fetching using Laravel resource. I read in the documentation here how you are supposed to do it, so I did:

ScanResource.php

class ScanResource extends JsonResource
{
public static $wrap = 'scan';
/**
 * Transform the resource into an array.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function toArray($request)
{
    //return parent::toArray($request);
    return ['id' => $this->id,
                    'rftag' => $this->rftag,
                    'jc_number' => $this->jc_number,
                    'station' => $this->station,
                    'agent' => $this->agent,
                    'created_at' => $this->created_at->format('d/m/Y H:i:s'),
                    'updated_at' => $this->updated_at->format('d/m/Y'),];
}
}

AppServiceProvider.php

class AppServiceProvider extends ServiceProvider
{
public function boot()
{
    //Paginator::useBootstrapThree();
    Schema::defaultStringLength(191);
    //JsonResource::withoutWrapping();
    //Resource::withoutWrapping();
    ScanResource::withoutWrapping();
}

public function register()
{
    //
}
}

This is how I am trying to fetch the resource in my controller:

public function show($id)
{
    $product = ScanDetail::find($id);

    if (is_null($product)) {
        return $this->sendError('Scan details not found.');
    }

    return $this->sendResponse(new ScanResource($product), 'Scan info retrieved successfully.');
}

currently I am getting the following JSON with Postman:

{
"success": true,
"data": {
    "id": 1,
    "rftag": "E200203204205212165166",
    "jc_number": "15",
    "station": "Repairing",
    "agent": "kbailey",
    "created_at": "11/06/2020 01:29:53",
    "updated_at": "11/06/2020"
},
"message": "Scan info retrieved successfully."
}

But I want:

{
    "success": true,
    "scan": {
        "id": 1,
        "rftag": "E200203204205212165166",
        "jc_number": "15",
        "station": "Repairing",
        "agent": "kbailey",
        "created_at": "11/06/2020 01:29:53",
        "updated_at": "11/06/2020"
    },
    "message": "Scan info retrieved successfully."
}

I tried this, this, this, this and this. This is not what I am using, so I did not think it would work. I also tried modifying my toArray to:

return [
        'scan'=>['id' => $this->id,
                    'rftag' => $this->rftag,
                    'jc_number' => $this->jc_number,
                    'station' => $this->station,
                    'agent' => $this->agent,
                    'created_at' => $this->created_at->format('d/m/Y H:i:s'),
                    'updated_at' => $this->updated_at->format('d/m/Y'),]
        ];

but its giving the following JSON:

{
"success": true,
"data": {
    "scan": {
        "id": 1,
        "rftag": "E200203204205212165166",
        "jc_number": "15",
        "station": "Repairing",
        "agent": "kbailey",
        "created_at": "11/06/2020 01:29:53",
        "updated_at": "11/06/2020"
    }
},
"message": "Scan info retrieved successfully."
}

Again, not what I want since I will be fetching different resources from the database using api calls. So I want to customize the outer wrapper. Any assistance is/will be greatly appreciated. Thanks in advance.

TheWildHealer
  • 1,546
  • 1
  • 15
  • 26
Vincent H Guyo
  • 356
  • 6
  • 24

2 Answers2

2

You don't need to do anything in your AppServiceProvider.php

You just need to create ScanResource.php as below:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ScanResource extends JsonResource
{
    /**
     * The "data" wrapper that should be applied.
     *
     * @var string
     */

    public static $wrap = 'scan';
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {        
        return [
            'id' => $this->id,
            'rftag' => $this->rftag,
            'jc_number' => $this->jc_number,
            'station' => $this->station,
            'agent' => $this->agent,
            'created_at' => $this->created_at->format('d/m/Y H:i:s'),
            'updated_at' => $this->updated_at->format('d/m/Y')
        ];
    }
    public function with($request)
    {
        return [
            "success" => true,
            "message": "Scan info retrieved successfully."   
        ];
    }
}

And you need to use that ScanResource.php in the ScanController.php as below

// use the namespace at the top
use App\Http\Resources\UserResource;

// add the below action in the controller.
public function show($id)
{
     $scan= Scan::find($id);
     return new ScanResource($scan);
}

I have tested the code in my postman and I got below output

{
    "scan": {
        "id" : 7,
        "rftag" : "abcde",
        "jc_number" :"AB123FG" ,
        "station" : "abc",
        "agent" : "Stanton Satterfield",
        "created_at": "29/10/2021 05:18:42",
        "updated_at": "29/10/2021 05:18:42"
    },
    "success" : true,
    "message": "Scan info retrieved successfully."   
}
0

in your controller change to this

return ['scan' => YourResource::collection(YourModel::get())];

and in your AppServiceProvider.php file add this

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Http\Resources\Json\JsonResource; // this

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        JsonResource::withoutWrapping(); // and this
    }
}

hope it's work for you :)