Questions tagged [laravel-eloquent-resource]
35 questions
12
votes
2 answers
What is the difference between a json resource & resource collection? in Laravel
Can someone explain the difference between a ResourceCollection and JsonResource?
In Laravel 6 docs you can generate 2 different types of resources... ResourceCollection and…

Josh
- 305
- 4
- 15
8
votes
2 answers
Laravel Eloquent API Resources: remove "data" key from response (collection)
I've Eloquent API Resource UserResource. When I try run something like this code:
$users = User::paginate(10);
return UserResource::collection($users);
Response will be like this:
{
"data": [
{
"name": "Fatima Conroy",
…

Andreas Hunter
- 4,504
- 11
- 65
- 125
4
votes
1 answer
Pagination not showing after I use Resource to display data
I'm having issues with pagination after I use a resource to display data. Before this, I was using this code to display data, and the output shows the pagination as well.
Output
Controller
$All = Customers::with('order')->paginate(10);
return…

Choy
- 452
- 1
- 5
- 19
2
votes
1 answer
Return all relationship data in laravel
I want to retrieve data using eloquent model and return it in the following json response.
user[
transactions[],
clients[
ubications[],
contactos[],
plans[],
]
]
Currently I am receiving…

HAMZA HUMMAM
- 309
- 1
- 2
- 9
2
votes
3 answers
Adding filter to eloquent resource to attach relationship conditionally
Laravel 5.8
PHP 7.4
I want to load the relationships conditionally like
http://127.0.0.1:8000/api/posts
and
http://127.0.0.1:8000/api/posts/1 are my end points now, I want to load comments like
http://127.0.0.1:8000/api/posts/?include=comments…

Prafulla Kumar Sahu
- 9,321
- 11
- 68
- 105
2
votes
2 answers
how to secure routes laravel-5.8
hi I wants that only admin can access the filecontroller route, or user can't access by typing URL:
these are routes:
Route::group(['middleware' => ['web','auth']], function(){
Route::get('/', function () {
return view('welcome');
…

pro
- 609
- 4
- 17
- 40
1
vote
1 answer
Laravel LoadSum() base on relation return also model data, how can I prevent it?
I've faced an issue. When I tried to loadSum results based on relations, it also return the model data also. How can I archive only specific data? This is my query.
auth()->user()->loadSum('results', 'total_questions')->loadSum('results',…

MH Raihan
- 31
- 6
1
vote
1 answer
Even though the correct id is set in the href of the anchor tag, my controller takes an id one behind
What I have
blade.php file

gfit21x
- 141
- 9
1
vote
1 answer
Call loadMissing() in a isolated model replica
I have a question maybe a little silly. I'm trying to replicate and store a model instance in a variable to call the loadMissing method. The idea is to call the loadMissing method without being affected by the original model. Let me explain.
I have…

leo95batista
- 699
- 9
- 27
1
vote
1 answer
Laravel Eloquent: API Resources - How does it work, so that I can create a similar class?
Look at this example:
use App\Http\Resources\UserResource;
use App\Models\User;
Route::get('/user/{id}', function ($id) {
return new UserResource(User::findOrFail($id));
});
How does this internally work? Because at first glance we just return…

Philipp Mochine
- 4,351
- 10
- 36
- 68
1
vote
1 answer
Modify response structure using Eloquent: API Resources
I am newbie in Laravel. I am using laravel as a REST End Point, I use Eloquent: API Resources for transforming my data to JSON.
I am using Collection and Resource to parse my data came from my query. So below is the structure of my response:
data:
…

Sharad Soni
- 378
- 1
- 5
- 18
1
vote
2 answers
How to remove some values in collection when using Laravel API Resource
I'm in a blog project. I have this API Resource for my Post model
return [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
'date' => $this->date
];
but I don't want to get 'body' => $this->body when I get…

samir
- 159
- 5
- 13
0
votes
2 answers
withCount() is not working acccording to filters applied in when()
Have problem with this is withCount() not changing after when() is applied. For example, without when(), transaction_count is 5000, and after when() is applied, it still remains 5000. It should change according to the filter applied in…

web pakistan
- 444
- 1
- 4
- 16
0
votes
2 answers
eloquent attempt to read property on int
I want to ask for your help.
I'm studying laravel, i am using laravel 10, and I need to retrieve related data, the application and data that I'm working on are related to students in a school. I want to retrieve student data with their angkatan.…

HALIM
- 63
- 8
0
votes
0 answers
SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (Connection: mysql, SQL: select * from `admins` where `` = 1 limit 1)
I'm attempting to log in to the dashboard using a combination of email and password. However, I'm encountering an error that prevents me from redirecting to the dashboard. Despite checking the data with the dd function, which confirms that all the…

iyed ghodhbane
- 13
- 1
- 3