107 questions
0
votes
1
answer
107
views
Can I display nested relationships in Laravel Resources?
I have a fairly common setup - 2 main models and 1 pivot with additional information regarding their relationship. I'm using Laravel API Resources and they work great, but I can't figure out a way how ...
0
votes
1
answer
123
views
Laravel Resource route is not holding any parameter on route list
GET|HEAD blogs ..................................................................................... blog.index › Blog\BlogController@index
POST blogs ...............................
-2
votes
2
answers
304
views
Laravel API DELETE Requests of Resource Controller and Getting 404 Not Found Error (for Second time Delete)
I'm encountering a peculiar issue in my Laravel API setup. I have a resource controller for managing student data, including deletion. When I make a DELETE request to http://127.0.0.1:8000/api/student/...
1
vote
1
answer
722
views
Why larastan raise error on relationLoaded method in resource file?
In resource file app/Http/Resources/PostResource.php I use method :
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class PostResource extends JsonResource
{
...
1
vote
2
answers
900
views
How to structure Laravel API resources folder properly [closed]
I wonder what are the best practices that the community have about the architecture of Laravel's Resources.
I started using it to avoid sending unnecessary data to certain routes of the API.
I ...
0
votes
3
answers
2k
views
How to get a single field from a Laravel whenLoaded resource
I am using a Laravel Resource to return my object. The object has optional relationships. My Resource code so far is below and is working as expected:
public function toArray(Request $request): array
{...
0
votes
1
answer
136
views
What can be used instead of whenLoaded in Resource when data are set with setAttribute?
In PostCrudRepository class I read mediaProps from other class and write as Attribute:
public function get(int $id): Post
{
$post = Post::getById($id)->firstOrFail();
$mediaStorage= App::...
0
votes
1
answer
821
views
append attribute to resurce from resource::make in laravel
I am trying to build a laravel api route to return users data. I got some data stored in txt file. To avoid accessing txt many times, the txt file is read in controller but not in resource.
However, ...
1
vote
0
answers
554
views
Laravel Resource conditional fields
In Laravel I'm generating serialized responses using resource classes. I have the following resources: JobOffer and Business. Here is JobOfferResource::toArray method
public function toArray(Request $...
0
votes
2
answers
911
views
Laravel resource performance
I'm using laravel framework and now I'm thinking that if laravel resource reduce the performance? Because it at first select all columns (select * from TABLE) then put the result in loop of resource. ...
1
vote
0
answers
737
views
Laravel Api resource not returning the data specified on the resource
In controller I have added a login method and when the login will success it will return the data only those which are included in a resource. But it is returning all data. It was working till morning ...
-2
votes
1
answer
540
views
in laravel , what is the differace between Route::resource and Route::get?
in laravel 9 , what is the difference between
Route::resource('/blog',[PostController::class);
and
Route::get('/blog',[PostController::class,'index']);
?
1
vote
0
answers
33
views
I have ItemResource reference in data written into redis
I use resources on api site to extend data(labels, formatting, related data ...).
But when I used ItemResource writing into redis :
$activeItems = Item::with('creator')->getByPublished(true)->...
1
vote
2
answers
2k
views
How to access a Pivot table from an API Resource in Laravel 9
I am building an API using Resources. I have a Product (Chocolate Cake) that can be linked to a Property (Allergies) that has Properties_property (Glutten) that need to be shown in different orders ...
1
vote
1
answer
58
views
Filter a model before you create an resource Collection
I am looking for a way that my ProductResource Collection will only include products with active prices.
In my routes/api.php am using a Collection as so:
Route::get('/product', function (Request $...
1
vote
2
answers
989
views
How to fix Laravel resource is not working properly in blade view?
I'm confused why casted props are inaccessible in the blade file. When I try to check it in the controller its showing properly.
Here's the JSON shown in the browser: return $users; (here status is ...
0
votes
1
answer
1k
views
Single api call vs multiple calls per page in SPA [closed]
I'm totally new to SPAs so I need to find the best approach for loading data in front-end.
I'm using React and Next for the front-end and Laravel for the back-end.
Currently I am making only one ...
0
votes
1
answer
1k
views
how to use collection on same model laravel resources
We are currently developing a feature in codotto.com where a user can comment on an IT meetup. Each comment can have an answer to it. We are only allowing for one-level deep answers, so something like:...
1
vote
1
answer
891
views
Update and Edit on Resource Controller: Can't Find Query Results for Model
I have a resource controller in my Laravel app, where all resource functions are placed. So far I have the creation and store functions working fine, but it falls on edit() and update(). As far as I ...
1
vote
2
answers
2k
views
Laravel: resource with nested self relationship
I've got a Category resource that looks like this:
<?php
namespace Domain\Category\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ...
0
votes
1
answer
5k
views
Laravel: Get the correct value of an enum field on a resource class
I'm using PHP8.1 and Laravel 9 for a project in which I've got the following enum:
enum OrderStatuses : string
{
case New = 'new';
case Pending = 'pending';
case Canceled ...
2
votes
1
answer
224
views
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 ...
1
vote
2
answers
1k
views
Laravel Different Views Folder
I have downloaded a project and I noticed that the project use views from different directory not the usual directory which are
\resources\views
but instead it create a new folder name themes and ...
0
votes
1
answer
131
views
Why using additive paramerer in Resource collection raised error?
In laravel 9 app I want to add additive paramerer into Resource and looking at this
Laravel 5.6 - Pass additional parameters to API Resource?
branch I remade in app/Http/Resources/CurrencyResource.php ...
0
votes
1
answer
97
views
How to add image props array in resouce?
In laravel 9 I want to join $CMSItem object and properties of related image(I use spatie/laravel-medialibrary)
in one resource, having in control :
$CMSItem = CMSItem
::getById($cms_item_id)
-&...
1
vote
1
answer
515
views
Soft deleted instance is not retrieved by Laravel Resource
I have two models related to each other with a one-to-one relationship.
Model A belongs to model B and when an instance of model B is returned to the frontend, the Resource of the model B includes the ...
0
votes
2
answers
16k
views
Laravel API resource paginate sub data
I'm trying to understand Laravel API resource and would like to know how I could use pagination with a subset of my data. For example: I have a book model and to return all the books available I could ...
0
votes
1
answer
550
views
Laravel groupBy in collection not working
I am trying to group the result with the status name inside. I am not doing groupBy after collection because I am using resource to return data, there are some logic inside resource. Grouping data ...
3
votes
0
answers
356
views
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 ...
0
votes
1
answer
340
views
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 ...
0
votes
1
answer
1k
views
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 ...
0
votes
1
answer
574
views
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 ...
1
vote
2
answers
5k
views
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('\...
1
vote
0
answers
382
views
Laravel Many to one in Resource
I use laravel 8 & have 3 table:
Products, ProductPrice & ProductsPublisher:
this is my Products model for this relationship:
public function lastPrice(){
return $this->hasMany(...
2
votes
1
answer
1k
views
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 ...
0
votes
1
answer
1k
views
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;
...
0
votes
1
answer
139
views
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.
...
1
vote
1
answer
2k
views
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 company_logo
CompanyResource::...
1
vote
1
answer
265
views
is it possible to return different models in an API response
This is my first time posting here so please pardon my errors:
I have a search functionality whose route is:
Route::get('/search', 'SearchController@index');
Currently, I have an eloquent ...
-1
votes
1
answer
213
views
Route [$products.edit] not defined
@foreach($products as $product)
<tr>
<td>{{ $product->id }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->job_category }}&...
2
votes
1
answer
3k
views
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 ...
1
vote
2
answers
6k
views
Whenever I use Laravel resource with paginate method, I get a response with "links" and "meta" keys. I want to change these keys to a new format
I used laravel resources for my api responses and added paginate method. Upon using paginate method I always get a result like this where laravel by default gives three keys namely "data", &...
0
votes
1
answer
24k
views
Attempt to read property "name" on null in Laravel 8 [duplicate]
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 ...
0
votes
0
answers
3k
views
How can I modify the Laravel API Resource collection response to include another 'data' wrapper with in the 'data' wrapper?
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class LanguageCollection extends ResourceCollection
{
public $collects = ...
8
votes
8
answers
9k
views
How to remove links in the meta object of Laravel paginate response?
Laravel default paginate gave me the response with default format for pagination, but I want to remove links in the meta object at the page response
I used below code to fetch the page data:
public ...
0
votes
1
answer
2k
views
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 ...
0
votes
1
answer
962
views
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 ...
0
votes
1
answer
2k
views
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 ...
0
votes
0
answers
4k
views
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 ...
1
vote
1
answer
2k
views
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 = [
...