'Laravel api Resource Returns null field values for show() action
I am using Laravel 9 and I am going nuts trying to figure out why the show() method of the API resource returns null for all fields when using the API resource controller, but returns the proper values when using individual API routes (e.g., route:get(....).
My API route using a resource controller in api.php is
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class InnAvoidsResource extends JsonResourceRoute::apiresource('inn-avoids',InnAvoidsController::class);
The InnAvoidsResource resource file contains
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class InnAvoidsResource extends JsonResourcepublic
function toArray($request)
{
return [
'id' => $this->id,
'position' => $this->position,
'avoid' => $this->avoid,
'definition' => $this->definition,
];
}
}
The show() method of the InnAvoidscontroller resource controller is
public function show(InnAvoids $innAvoids)
{
return new InnAvoidsResource($innAvoids);
}
If I go to .../api/inn-Avoids/3 for example, I get...
data:
id: null
position: null
avoid: null
definition: null
However, If I switch my routes to use individual API routes for the different actions and use the following route for the show() in api.php. It works. For example, if I change the route to.
Route::get('/inn-avoids/{id}',function($id) {
return new InnAvoidsResource(InnAvoids::findOrFail($id));});
I get the following expected output (I seeded the DB with faker).
data:
id: 3
position: "Sed vel."
avoid: "Eos fugit ea quaerat."
definition: "Ducimus tempora quos corrupti voluptatibus et amet numquam. Voluptas quam iste quia vel. Magnam dignissimos sint quam eaque. Amet magni aut totam quo autem minus vero."
The index() method of the controller works for both cases, i.e., using the resource controller or ::get route in the api.php file.
Also, when I dd() the $innAvoids parameter in the show() method of the API resource controller, I get a connection and table values of null.
App\Models\InnAvoids {#1212
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
incrementing: true
The connection should be "mysql" and table, InnAvoids.
I have another application where I used the same API routing setup and it works fine.
I can't figure out what is causing the behavior described above. Why would I get a single resource using Route:GET but all nulls if I use Route::apiresource routes?
I also found that the connection and table are null when I dump $this in the resource.
App\Http\Resources\UserAvoidResource {#1219 ▼
resource: App\Models\userAvoid {#1224 ▼
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#attributeCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
+with: []
+additional: []
}
Solution 1:[1]
Make sure that your resource extends JsonResource
not ResourceCollection
like this
use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
}
for more info check here the first example
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |