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?
via user2926430