1

I'm trying to return an object from my Laravel api routes, but all that's returned is an empty array.

My model looks like this:

    class MobilePageStats extends Model
    {
        //
        private $score;
        private $mobileFriendly;
        private $numberRobotedResources;
        private $numberTransientFetchFailureResources;
        private $transientFetchFailureUrls;
        private $cms;
        private $ruleResults;

        /**
         * MobilePageStats constructor.
         * @param int $score
         * @param bool $mobileFriendly
         * @param int $numberRobotedResources
         * @param int $numberTransientFetchFailureResources
         * @param array $transientFetchFailureUrls
         * @param string $cms
         * @param array $ruleResults
         */
        public function __construct(
            $score,
            $mobileFriendly,
            $numberRobotedResources,
            $numberTransientFetchFailureResources,
            $transientFetchFailureUrls,
            $cms,
            $ruleResults
        ) {
            $this->score = $score;
            $this->mobileFriendly = $mobileFriendly;
            $this->numberRobotedResources = $numberRobotedResources;
            $this->numberTransientFetchFailureResources = $numberTransientFetchFailureResources;
            $this->transientFetchFailureUrls = $transientFetchFailureUrls;
            $this->cms = $cms;
            $this->ruleResults = $ruleResults;
        }

And I also got getters on everything.

I set all the data in my controller with the constructor, in this function:

public function getData() {
            $cms = "";
            $score = $this->data->ruleGroups->USABILITY->score;
            $mobileFriendly = $this->data->ruleGroups->USABILITY->pass;
            if(isset($this->data->pageStats->numberRobotedResources)){
                $numberRobotedResources = $this->data->pageStats->numberRobotedResources;
            }else{
                $numberRobotedResources = '';
            }
            if(isset($this->data->pageStats->numberTransientFetchFailureResources)){
                $numberTransientFetchFailureResources = $this->data->pageStats->numberTransientFetchFailureResources;
            }else{
                $numberTransientFetchFailureResources = '';
            }
            if(isset($this->data->pageStats->transientFetchFailureUrls)){
                $transientFetchFailureUrls =  $this->data->pageStats->transientFetchFailureUrls;
            }else{
                $transientFetchFailureUrls = '';
            }
            if(isset($this->data->pageStats->cms)){
                $cms = $this->data->pageStats->cms;
                if($cms != 'WORDPRESS' && $cms != 'JOOMLA'){
                    $cms = $this->checkCMS();
                }
            }
            $cvp = $this->getConfigureViewport();
            $fontSizes = $this->getUseLegibleFontSizes();
            $avoidPlugins = $this->getAvoidPlugins();
            $sizeToViewport = $this->getSizeContentToViewport();
            $tapTargets = $this->getSizeTapTargetsAppropriately();
            $ruleResults = [$cvp, $fontSizes, $avoidPlugins, $sizeToViewport, $tapTargets];


            $mobilePageStats = new MobilePageStats($score, $mobileFriendly, $numberRobotedResources,
                $numberTransientFetchFailureResources, $transientFetchFailureUrls, $cms, $ruleResults);

            return $mobilePageStats;

        }

In my API routes I then try to return the model like this:

Route::get('/mobilePageSpeed', function(Request $request){
    $data = new PageSpeedMobileController($request->url);
    return response($data->getData());
});

But all I get returned when I make the request is:

<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">[]</pre>
</body>

Why is the object not returned? I know it contains data, because I can print it. But can't send it?

I have tried both repsonse()->json($data->getData()); And json_encode($data->getData()), but they give me the same result? I just can't seem to find a solution that works.

So how do I return objects from Laravel Api?

2 Answers 2

1

The answer would be to json_encode every single object after you cast them to arrays. Should work :)

Sign up to request clarification or add additional context in comments.

1 Comment

Nice, that did the trick!! But I still had to return it as an json response
0

You override the original Model constructor with your version, the model is not filled with attributes nor booted.

I dunno what you want to achieve but if you extends a class from Eloquent\Model you cannot override is own costructor without calling the original one, i.e.:

 public function __construct(array $attributes = [])
 {
    parent::__construct($attributes);

4 Comments

I tryed to remove the extends Model, cause I really don't need the extension of Model, as far as I know. But this gave the same result. So I don't think that this will solve my issue.
I dont want to be rude but If its the same for you to extends from Model or not I cant follow you: I have no time for a game of try and retry. I pointed an important issue of your code, do your homework and your tries and if you have still problems post another question.
I get your point, and I tried to add parent construct, but this did not solve my issue. The reason that I don't think is necessary for me to extend model, is because it's only generated object, that I don't save it in an database or anything, its purpose is only to hold my data. I am thankful for that you used your time to look into my issue, but I did do my homework, tried almost every solution that I could find, but it's still not working.
I can add only an advice: look in the Eloquent Model source for the __toString() and toJson() functions. If you return a response() you have to give to that function a string or a array.

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.