Questions tagged [laravel-resource]

For more information visit Laravel resources reference.

107 questions
0
votes
3 answers

Laravel Apply middlware on specific route of resource route controller

I want to apply middleware on create route of resource controller but confused how to set middleware. Normally we can add middleware like this Route::get('api/users/{user}', function (App\Models\User $user) { return…
0
votes
1 answer

Laravel format resource collection response error Property [product_id] does not exist on this collection instance

In laravel we can format our json response from resource class as seen below class ProductsResource extends JsonResource { public function toArray($request) { return [ 'id'=> $this->product_id , …
Khal
  • 219
  • 5
  • 19
0
votes
1 answer

Test rest api return 302

I use rest API. For example, requwest http://api/events/?api_token=KFvcc10H4l return me json. But in test I get error 302 /** * @return void */ public function testReturnOneEventsItem() { $this->get(…
sapef44489
  • 69
  • 9
0
votes
2 answers

Laravel 6 API RESTful with many to many relationship filtered

I'm developing a Multilanguage API with laravel 6 and I've got this situation in my database: Categories id other not relevants fields Languages: id name code Category_Language id language_id category_id name --> this is the name of the category…
BobbyLinux
  • 140
  • 1
  • 12
0
votes
1 answer

Laravel: Resources with json field

I am on Laravel 7.x and I have two models (CustomerOrder composed of many CustomerOrderLines) with parent - child relationship. Parent (CustomerOrder) model has a json type field among its fields. CustomerOrderResource.php: return [ 'id' =>…
Lakmal Premaratne
  • 1,159
  • 7
  • 18
  • 34
0
votes
2 answers

Conditionally loaded data in API resource: How to "pass" a condition?

I got a little issue to solve. In my app I am handling with a lot of Models and each model does have something like: ModelResource ModelResourceCollection ModelResourceOverview ModelResourceOverviewCollection The reason is: Sometimes I don't need…
SPQRInc
  • 162
  • 4
  • 23
  • 64
0
votes
2 answers

Array is returned as an object using resource

something strange is going on. I got an array like this: => [ "optionalinformation" => [ "domain" => [ "type" => "string", ], ], ] This array is used by a resource and if I use tinker to check this resource like…
SPQRInc
  • 162
  • 4
  • 23
  • 64
0
votes
1 answer

Trying to get property 'resource' of non-object in PaginatedResourceResponse.php after Laravel-update

actually I am getting this error after updating Laravel from v7.5.1 to v7.6.2 "message": "Trying to get property 'resource' of non-object", "exception": "ErrorException", "file":…
SPQRInc
  • 162
  • 4
  • 23
  • 64
0
votes
1 answer

How to use same Laravel Resource for different API response

i want to make two different functions on resource so that i can get a two different response .i.e i want the resource to return data without image and with image. public function toArray($request) { return [ 'id' =>…
Ravi Sigdel
  • 161
  • 10
0
votes
1 answer

Laravel api resource only if its owned by user

Lets say a user belongs to a company and each company has many projects. Now I wanna make an API resource for projects. For this, I wanna work with https://laravel.com/docs/6.x/eloquent-resources For example for the show method of the controller, I…
bernhardh
  • 3,137
  • 10
  • 42
  • 77
0
votes
1 answer

Reading data from in vue/cli from Laravel ResourceCollection

In Laravel 6 backend rest api app I use ResourceCollection and Resourcem defintion like :
mstdmstd
  • 2,195
  • 17
  • 63
  • 140
0
votes
1 answer

Laravel api resource with method does't work

This is my resource file: namespace App\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; class Wpis extends JsonResource { public function toArray($request) { return parent::toArray($request); } public…
Yersey
  • 3
  • 2
0
votes
1 answer

Laravel Api Resource

I'm trying to create an API for my data tables using Laravel's resource. I have three models with relationships. Every time I hit my api routes to check the result I'm getting a null value in my sub_specializations. Here's the result already JSON…
eugenebermudez
  • 175
  • 1
  • 1
  • 11
0
votes
1 answer

How can I automatically add conditions in the method of a resource controller in Laravel?

I am trying to add a if condition when resource auto generates. When I run php artisan make:controller SomeController -r, I want to generate the following, class SomeController extends Controller { public function index() { if…
Aushraful
  • 132
  • 1
  • 7
0
votes
2 answers

Call to a member function first() on null in laravel resource

i try to build a customized response in my resource like this: class ApplicationResource extends JsonResource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request …
Vahid Saberi
  • 105
  • 1
  • 2
  • 12