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

returning full path when using as json #112

Open
mouhsinelonly opened this issue Jun 20, 2016 · 2 comments
Open

returning full path when using as json #112

mouhsinelonly opened this issue Jun 20, 2016 · 2 comments

Comments

@mouhsinelonly
Copy link

i found in stapler docs that it should return attachment sizes with full path when returning data as json, but it does not seem to work with laravel-stapler

https://github.com/CodeSleeve/stapler/blob/master/docs/attachments.md

@tabennett
Copy link
Contributor

The stapler trait doesn't currently 'automagically' add it when you cast to JSON, so you'll need to handle this yourself. One option is to add it to the toArray() method of your model like this:

/**
     * Convert the model instance to an array.
     *
     * @return array
     */
    public function toArray()
    {
        return array_merge(parent::toArray(), ['avatar' => $this->attachments['avatar']]);
    }

It'll then be encoded to JSON along with the rest of the model attributes/relationships.

@draaslan
Copy link

Also there is an another method like below.

You can add this to your model.

public function getFullPathAttribute() { return $this->avatar->url(); }

And then add 'full_path' to the $appends to bind it to every json data.

protected $appends = ['full_path'];

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

No branches or pull requests

3 participants