For more information visit Laravel resources reference.
Questions tagged [laravel-resource]
107 questions
18
votes
2 answers
Laravel transformer vs resource
I am little bit confused about the difference between Laravel eloquent resource and transformers. When i should use resource and when i should use transformers. Did some research on it from laravel documentation and medium article but its look like…

Emtiaz Zahid
- 2,645
- 2
- 19
- 35
10
votes
3 answers
Laravel - How to convert API Resource to array recursively?
I'm using Laravel API Resource and want to convert all parts of my instance to an array.
In my PreorderResource.php:
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function…

Alexandre Thebaldi
- 4,546
- 6
- 41
- 55
5
votes
3 answers
Multiple API Resources in one call using laravel
I am using API Resources for laravel to transform resource to array for an API call,and its working fine,Is is possible that i can retrieve data of multiple models in one call ? As to get JSON data of users along with Pages JSON ? Or i need a…

Khirad Zahra
- 843
- 2
- 17
- 42
4
votes
1 answer
How to test Laravel resource
I am testing an API endpoint returned Laravel resource with pagination
public function test_showing_all_plans(): void
{
$plans = Plan::where('is_active', true)
->paginate(10);
$resource = PlansResource::collection($plans);
…

TheGeeky
- 962
- 1
- 12
- 34
4
votes
1 answer
Is It Possible To Use Laravel Resource With [Web] Guard?
I'm Using Ajax request for fetching data, It return a Json Response Data which is the same data that I will return when creating API .
The question is: Can I Create Laravel Resource Class and Return that data using (web), (API) Guards ?
auth.php…

Faid
- 554
- 1
- 5
- 18
4
votes
1 answer
Laravel resource conditional return
I've got simple laravel resource:

gileneusz
- 1,435
- 8
- 30
- 51
3
votes
0 answers
Why do we we pass $request to toArray() if we dont use it?
I am new to Laravel. I am currently building a resource. I was just wondering what is the purpose of passing in the $request object if we use the $this variable when using resources. Below is an example of my code.
class PetitionResource extends…

json2021
- 2,146
- 2
- 14
- 28
3
votes
4 answers
Exception Creating New Resource in Laravel Nova - Class 'App\Post' not found
I have a brand new installation of Laravel Nova. The dashboard comes up fine. But when I add a new resource using php artisan nova:resource Post and reload the dashboard, it's throwing an error. When I remove the offending model from Nova folder,…

Rick Leckrone
- 65
- 3
- 8
2
votes
1 answer
Laravel: resource with nested self relationship
I've got a Category resource that looks like this:

MrCujo
- 1,218
- 3
- 31
- 56
2
votes
1 answer
Why not mix string and numeric keys when using mergeWhen() with Laravel Resources
As one can read in the Laravel documentation, the following warning is stated:
The mergeWhen method should not be used within arrays that mix string and numeric keys. Furthermore, it should not be used within arrays with numeric keys that are not…

Robin Bastiaan
- 572
- 2
- 8
- 21
2
votes
1 answer
Laravel custom attributes loads relationships even when attribute is not asked
I have a custom attribute that calculates the squad name (to make our frontend team lives easier).
This requires a relation to be loaded and even if the attribute is not being called/asked (this happens with spatie query builder, an allowedAppends…

Wanjia
- 799
- 5
- 19
2
votes
1 answer
Why my resource do not return all meta data having "with" method?
In lumen 8 app I use resources and reading here
https://laravel.com/docs/8.x/eloquent-resources
I try to use “with” method, as I want to add some meta data to any request and I have no
this meta data in returned data :

Petro Gromovo
- 1,755
- 5
- 33
- 91
2
votes
1 answer
How can I create a Laravel Resource relationship for a belongsTo?
I have created a UserResource that is successfully returning all of my user attributes, including the organization it belongs to. It looks something like this:
Resources/User.php
return [
'type' => 'users',
'id' => (string)$this->id,
…

Damon
- 4,151
- 13
- 52
- 108
2
votes
1 answer
Trying to return pivot data in Resource Laravel
I am trying to return pivot data to a resource.
The pivot table works, I can add and remove entrys like expected, but I am not able to get the user_id returned in ActivityResource...
In the Laravel Documentation it looks so easy, am I missing…

combi35
- 355
- 4
- 14
2
votes
1 answer
Resource response is not wrapped with "data"
I am very curious, why my resource response is not wrapped in data:
This is my resource:
App\Http\Resources\CategoryResource Object
(
[resource] => stdClass Object
(
[id] => 12
[title] => Category
…

SPQRInc
- 162
- 4
- 23
- 64