Support the ongoing development of Laravel.io →
posted 9 years ago
Views

Hi my issue is as follows I have a controller as follows

public function index()
    {
        $model = new Chart();
        $chart = $model->getChart();
        return view('chart', ['chart' => $chart]);
    }

my models getChart function

public function getChart()
    {
        $T = 0;
        $id = 3;
        $chart = self::calculateResult($id, $T);
        return $chart;
        //return 'Have a cup of tea!';
    }

if the function above is modified so as to return the string (last example) the code works and outputs the resulting string in the view

@extends('layout.default')
@section('content')
<div class="container">
    <div class="chart-container">
        <p class="chart">{{$chart}}</p>
    </div>
</div>
@stop

Additional the trivial function calculateResult returns a cordinate in an array

'x' => 27.788932467,
'y' => -38.12342134,

My problem is that this causes an error as follows

ErrorException in c74620b2dbf98558a0a1c8a9bec49c51 line 4: Trying to get property of non-object (View: /var/www/local.chart.com/resources/views/chart.blade.php)

I've tried return json_encode($chart), response()->json($chart) and cant get the data correctly into my view so i can use {{$chart->x}} what am i missing here?

ps I am using Lumen. Also when i json_encoded the array it worked with $chart in the view but $chart->x produces this error

htmlentities() expects parameter 1 to be string, array given (View: /var/www/local.chart.com/resources/views/chart.blade.php)
Last updated 2 years ago.
0

on the template $chart is an array..

in blade you can echo string only and blade will run htmlentities on every variable it's passed.

you can however do this on the template

<p class="chart">{{ json_encode($chart) }}</p>
Last updated 9 years ago.
0

You are producing an array, arrays are accessed via [], not via ->. So either you return an object from your calculateResult() method (e.g. with stdClass) or you simply use

{{ $chart['x'] }}

As far as I can see this has nothing to do with json whatsoever.

0

ftiersch said:

You are producing an array, arrays are accessed via [], not via ->. So either you return an object from your calculateResult() method (e.g. with stdClass) or you simply use

{{ $chart['x'] }}

As far as I can see this has nothing to do with json whatsoever.

After writing php for well over a decade i should of got that. Thanks ftiersch, now it makes sense. I'd based above on an eleoquent solution but wanted to run calcultions and render results. I'll convert my calculateResults class to return the data as an object.

In the end i just cast the array to an object in the Models return statement ie

return (object) $chart;
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.