Laravel 5.6 - Pass additional parameters to API Resource?

前端 未结 6 954
天涯浪人
天涯浪人 2020-12-28 17:26

A Laravel API Resource can be either a single resource or a collection. In some cases, additional parameters are required to be passed to the resource/collection from the co

相关标签:
6条回答
  • 2020-12-28 18:03

    You can pass the extra parameters in as part of the call to the API endpoint. You can then access the parameters with the $request object (for your example) in the UserResource.

    For example, if you call the endpoint from a client such a web browser, axios, etc. using something like:

    http://localhost:3000/api/users?apple=true
    

    this will make the parameter apple with a value of true available in the controller. Without any other action on your part it will then also be accessible in the toArray($request) of the UserResource. You can access it similar to:

    public function toArray($request) {
          $isApple = $request->apple;
    
            return [
                'id'     => (int) $this->id, 
                'name'   => $this->name,
                'fruit'  => $isApple ? 'apple' : 'banana',
            ];
        }
    
    0 讨论(0)
  • 2020-12-28 18:03

    To works with Laravel 5.7, I made some changes in relation to Wonka's answer

    UserResource

        class UserResource extends Resource{
    
            protected $foo;
    
            public function foo($value){
                $this->foo = $value;
                return $this;
            }
    
            public function toArray($request){
                return [
                    'id' => $this->id,
                    'name' => $this->name,
                    'foo' => $this->foo,
                 ];
            }
    
            public static function collection($resource){
                return new UserResourceCollection($resource, get_called_class());
            }
        }
    

    UserCollection

        class UserResourceCollection extends AnonymousResourceCollection {
    
            protected $foo;
    
            public function foo($value){
                $this->foo = $value;
                return $this;
            }
    
            public function toArray($request){
                return $this->collection->map(function(UserResource $resource) use($request){
                    return $resource->foo($this->foo)->toArray($request);
            })->all();
    
            }
        }
    
    0 讨论(0)
  • 2020-12-28 18:05

    I solved my issue, by just retrieving the $request->get('param') inside the

    public function toArray($request){ 
    
       $param = $request->get('param');
    
       ... 
    
    } 
    

    Instead of passing the param through the Resource.

    0 讨论(0)
  • 2020-12-28 18:06

    The following approach worked for me:

    UserResource

    class UserResource extends Resource{
    
        protected $foo;
    
        public function foo($value){
            $this->foo = $value;
            return $this;
        }
    
        public function toArray($request){
            return [
                'id' => $this->id,
                'name' => $this->name,
                'foo' => $this->foo,
             ];
        }
    
        public static function collection($resource){
            return new UserResourceCollection($resource);
        }
    }
    

    UserCollection

    class UserResourceCollection extends ResourceCollection{
    
        protected $foo;
    
        public function foo($value){
            $this->foo = $value;
            return $this;
        }
    
        public function toArray($request){
            return $this->collection->map(function(UserResource $resource) use($request){
                return $resource->foo($this->foo)->toArray($request);
        })->all();
    
            // or use HigherOrderCollectionProxy
            // return $this->collection->each->foo($this->foo)->map->toArray($request)->all()
    
            // or simple
            // $this->collection->each->foo($this->foo);
            // return parent::toArray($request);
        }
    }
    

    Different ways to pass the additional parameter

    (new UserResource($user))->foo('bar');
    (new UserResourceCollection($user))->foo('bar');
    
    UserResource::make($user)->foo('bar');
    UserResourceCollection::make($users)->foo('bar');
    UserResource::collection($users)->foo('bar');
    
    0 讨论(0)
  • 2020-12-28 18:20

    You can use laravel 8

    for store function Additional

        return (UserResource::make(User::find($user->id)))
                ->additional([
                    'message'=>[
                        ['user by name: '.$user->name.' created successfull.']
                    ]
                ])->response()->setStatusCode(201);
    
    0 讨论(0)
  • 2020-12-28 18:21

    This simple trick worked for me in Laravel 5.8 :)

    Controller

    $user = User::find($user->id);
    $user->access_token = $tokenResult->accessToken; // Add additional data
    return new ProfileResource($user);
    

    Resource

    public function toArray($request)
    {
        return [
            'id'            => $this->id,
            'picture'       => $this->picture,
            'first_name'    => $this->first_name,
            'last_name'     => $this->last_name,
            'active'        => $this->active,
            'access_token'  => isset($this->access_token) ? $this->access_token : '', // Additional data
        ];
    }
    
    0 讨论(0)
提交回复
热议问题