Skip to content

returning full path when using as json #112

Open

Description

@mouhsinelonly

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

Activity

  1. tabennett commented on Jun 20, 2016

    @tabennett
    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.

  2. draaslan commented on Jul 26, 2017

    @draaslan

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions