7

I want (for project reason), to create an array in a class controller and pass it to a resource. Consider in my controller class this method:

public function getExample(){
  $attribute=array('otherInfo'=>'info');

  return new ExampleResource($attribute);
}

and I in my class I would write sominthing like ExampleResource with:

public function toArray($request){
return[
'info' => $this->info
];

}

How I can convert the value $attribute to perform this operation return new ExampleResource($attribute); ?

Please do not suggest me to insert the field info in the model, this attribute can came from only from the external, from the controller and do not belong to the model in database.

class ExampleResource extends Resource
{
    private $info;
    /**
     * 
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
     public function __construct($info)
     {
         $this->$info = $info;
     }


    public function toArray($request)
    {
        return[
          'info'=>$this->$info,
          'id' => $this->id
          ];
          }
        }
1
  • Your question is quiet confusing. You want an array to return new ExampleResource($attribute);? It almost sounds like youre trying to create a model factory? Commented Jan 19, 2018 at 14:02

2 Answers 2

8

Add constructor to the resource class:

public function __construct($resource, $attribute)
{
    $this->resource = $resource;
    $this->attribute = $attribute;
}

Then in toArray():

return [
    'info' => $this->attribute,
    'created' => $this->created_at
];

And use it:

return new ExampleResource(Model::find($id), $attribute);
Sign up to request clarification or add additional context in comments.

9 Comments

@user8958651 I've tested this code before posting, I've used array too. Please tell me what line exactly throws an error? Also, please show the exact code you're trying to use now.
As you can see in the edit file the error is in the constructor: $this->$info= $info;
@user8958651 change $this->$info = $info; to $this->info = $info; and do the same in the toArray(). I've just copy-pasted $attribute and didn't remove the $ sign.
with $this->info=$info into costructor, and with 'info'=>$this->info into toArray , the execution return the error "Trying to get property of non-object" on the method of public function __get($key) { return $this->resource->{$key}; } in the library Http\Resources\DelegatesToResource.php
@user8958651 please update code in the question with current one.
|
0

Resources are intended to be used to easily transform your models into JSON.

Take a look at this example:

use App\User;
use App\Http\Resources\UserResource;

Route::get('/user', function () {
    return new UserResource(User::find(1));
});

You just want to return an array of data so you should just return the array, it will be automatically turned into JSON:

Route::get('/info', function () {
    return ['info' => 'info ...'];
});

For more informations check the docs here

1 Comment

what if I want to get the array without return it first ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.