Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Model::preventAccessingMissingAttributes()" Causes Exception During Pagination with ResourceCollection #52214

Closed
Karem-sobhy opened this issue Jul 21, 2024 · 6 comments · Fixed by #52305

Comments

@Karem-sobhy
Copy link
Contributor

Laravel Version

11.16.0

PHP Version

8.3.9

Database Driver & Version

MariaDB 11.4.2

Description

When using pagination with ResourceCollection while Model::preventAccessingMissingAttributes() is activated, an attempt to access resource attributes occurs. This access results in a violation of the prevent access to missing attributes policy, causing an exception to be thrown.

"message": "The attribute [resource] either does not exist or was not retrieved for model [App\\Models\\User].",
"exception": "Illuminate\\Database\\Eloquent\\MissingAttributeException"

Steps To Reproduce

  • install new laravel instance
  • create 1 user using the default seeder php artisan db:seed
  • in AppServiceProvider add in the register method this Model::shouldBeStrict(); or this Model::preventAccessingMissingAttributes();
  • make a UserCollection php artisan make:resource UserCollection
  • in any route now return this return new UserCollection(User::paginate());
@Karem-sobhy Karem-sobhy changed the title "Model::preventAccessingMissingAttributes() Causes Exception During Pagination with ResourceCollection "Model::preventAccessingMissingAttributes()" Causes Exception During Pagination with ResourceCollection Jul 22, 2024
Copy link

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@lrljoe
Copy link

lrljoe commented Jul 28, 2024

Hmm, in a route:

return UserResource::collection(User::paginate(2))

Then UserResource looks like:

class UserResource extends \Illuminate\Http\Resources\Json\JsonResource
{
    /**
     * Indicates if the resource's collection keys should be preserved.
     *
     * @var bool
     */
    public $preserveKeys = true;

    public function toArray(\Illuminate\Http\Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }

}

Does that work? Trying to narrow down what the issue is!

@Karem-sobhy
Copy link
Contributor Author

@lrljoe this will work the problem is not with the JsonResource it's with Illuminate\Http\Resources\Json\ResourceCollection because it tries to access resource attribute

the cause of the problem is in this line

return is_array($item) ? Arr::get($item, 'resource') : $item->resource;

but i don't have the experience to understand how to fix it

@Katalam
Copy link
Contributor

Katalam commented Jul 29, 2024

I tried to fix it. It will fix the problem. I don't really know if the scope of the test is ok. But the problem would be fixed with referenced PR

@Karem-sobhy
Copy link
Contributor Author

I tried to fix it. It will fix the problem. I don't really know if the scope of the test is ok. But the problem would be fixed with referenced PR

@Katalam I think u are using JsonResource with collection method and that didn't have the problem but did you try with ResourceCollection?

php artisan make:resource UserCollection
return new UserCollection(User::paginate());

@Katalam
Copy link
Contributor

Katalam commented Jul 29, 2024

I did. to fix the problem, but not to write the test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants