For more information visit Laravel resources reference.
Questions tagged [laravel-resource]
107 questions
0
votes
1 answer
How to create column aliases in a resource
I am creating an API using Laravel.
I created a table called transaction. I also created a resource called transactionresource.
I want to change the id column to be the key column in the API, and I don't want id to show in the API; instead key…

etranz
- 891
- 2
- 10
- 29
0
votes
1 answer
How can I filter data from relations in Laravel 8?
I have 3 tables business, service , service_venues. I need services of business having service_name = $search and and get venues of selected services. I am using services and resources method.
When there is search_keyword the data is not filtered.…

Jyothi
- 53
- 8
0
votes
1 answer
Pagination not working while retriving data with costom json function
I'm fetching data to paginate but pagination is not working, the fetch response is added below, if I return the query relation it retrieves correct data but when I pass it to the custom response function it fetch data only but not the pagination…

swatantra
- 383
- 1
- 7
- 18
0
votes
2 answers
Laravel - Relationship With Resources and whenLoaded Not Working
So I am trying to control output utilizing resources as I was told it's the best way to model the data for api output.
Customer Model
public function invoices () {
return $this->hasMany('\App\Models\Invoice');
}
Invoice Model:
public…

GrafixMastaMD
- 139
- 5
- 15
0
votes
1 answer
Laravel resource converting my associative array into a standard array
PHP 7.4.16 & Laravel 8.61
I have the following code in my Resource:
$arr = ['id' => $this->id, 'associations' => (object)[]];
foreach ($tas as $ta) {
$ta_id = $ta->id;
$arr['associations']->$ta_id = ['suppliers' => [], 'offices'…

Inigo EC
- 2,178
- 3
- 22
- 31
0
votes
1 answer
paginate relational resource in laravel api
I have two models, User and Comment, Each User can have multiple Comment, I can call paginate(3) on User and get 3 User in output per page but I need same thing as User for comments in UserCollection.
Is it possible to limit comments that comes by…

mohammad
- 7
- 6
0
votes
1 answer
Exclude fields from a Resource template Laravel 7 and 8
I have come across a solution that filters fields from a resource collection in the Controller CompanyController.php
E.g The code below returns all the values except…
0
votes
1 answer
Attempt to read property "name" on null in Laravel 8
I am working with laravel API and using model relationship and resource to get data, I don't know where is the problem that it gives such kind of error, I searched and tried the solutions, but the problem is still alive.
This is my controller…

Zia Yamin
- 942
- 2
- 10
- 34
0
votes
0 answers
0
votes
1 answer
Laravel edit shallow nested resource
I'm using the following resource using shallow nesting
Route::resource('organizations.emaildomains', 'OrganizationEmailDomainController', ['except' => ['show']])->shallow();
Got the following table with two records which is the result of an…

Tiago Martins Peres
- 14,289
- 18
- 86
- 145
0
votes
1 answer
Laravel resource index listing limited amount of records
I have two resources
Organizations
OrganizationUsers (which has FK to Users on user_id)
The User model
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
…

Tiago Martins Peres
- 14,289
- 18
- 86
- 145
0
votes
1 answer
Laravel Spatie Permissions Get a user resource with their roles and permissions
I am writing because I have spent hours of testing looking for a solution to a problem and I still have not found how to solve it. I am using Spatie Laravel Permission and it happens that I have 3 resources, one for user, another for role and…

leo95batista
- 699
- 9
- 27
0
votes
0 answers
Spatie Laravel Permission. Return API resource with user roles and permissions
I have a problem when returning an api resource. I am using Spatie Laravel Permission.
It happens that in my particular case I am trying to return an api resource with the details of the user and next to them the roles and permissions that the user…

leo95batista
- 699
- 9
- 27
0
votes
1 answer
Laravel cast model values when using Resource
When returning a model using resource created_at and updated_at casting is working fine,
but when i modify the toArray() function the casting is not working !
in my model :
protected $casts = [
'created_at' => 'datetime:Y-m-d:h',
…

derar sattouf
- 69
- 2
- 9
0
votes
0 answers
Laravel One to Many and Many to Many for the same Model
I am trying to make a task management system. Users can create a task and assign multiple users to the task. So the task table will have a user_id for identifying the user who created the task. There is a pivot table that consists of user_id and…

Kishan Suresh
- 45
- 8