You could just do something like
// In your controller
$sites = Sites::all();
$object = [];
foreach ($sites as $site) {
$object[] = ['name' => $site->name, 'lat' => $site->lat, 'long' => $site->long];
}
return View::make('your.view', ['sites' => $object]);
// In your view
var sites = {{ json_encode($object) }};
Thanks for the suggestion, unfortunately I'm getting
Undefined variable: object (View: blah/blah/sites.blade.php)
Any other tips?
How do you pass your data to view? Can you be more specific why it doesn't work?
Hi Mirago,
Sorry I can't provide much more info, as mentioned above I'm a rookie when it comes to this stuff so I'm still finding my feet.
It would appear that CaporalDead suggested creating a json object in the controller and then passing the object in to the view however somewhere along the line the process isn't working.
Unfortunately the best guidance I can provide is in my opening question, sorry :(
Cheers
Well you could do something like his example, however in here
var sites = {{ json_encode($object) }};
you should be using $sites instead of $object
Thanks Mirago, getting closer!
The page no longer breaks but I'm getting a js error in my leaflet application saying 'Cannot read property 'lat' of null' .
I think the problem is that my var sites2 = {{ json_encode($sites) }}; is being returned as
var sites2 = [{"long":"-0.10424942","lat":"51.52754211"},{"long":"-0.15128464","lat":"51.51632690"},{"long":"-0.07867187","lat":"51.51344299"},{"long":"-0.08433670","lat":"51.51824951"}];
when I think it should be returned as
var sites = [
[-40.99497,174.50808],
[41.30269,173.63696],
[-41.51285,173.53274]
];
Urgh I wish I picked any easier project to start on :)
Thanks for all your help everyone!
So just loop through that array as you did before with foreach. use $siteatt['lat'] instead of $siteatt->lat if its not ojbect
You're the man Mirago!
So to confirm what I ended up with In my Controller
$sites = Site::all();
return View::make('sites.sites', ['sites' =>$sites]);
In my view
var sites2 = [
@foreach ($sites as $siteatt)
[{{$siteatt['lat']}},
{{$siteatt['long']}}],
@endforeach
];
Thanks everyone for their help.
Sorry, didn't have time to answer you but I'm glad that @mirago could help you :).
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community