Support the ongoing development of Laravel.io →
Database Blade
Last updated 1 year ago.
0

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) }};
Last updated 1 year ago.
0

Thanks for the suggestion, unfortunately I'm getting

Undefined variable: object (View: blah/blah/sites.blade.php)

Any other tips?

Last updated 1 year ago.
0

How do you pass your data to view? Can you be more specific why it doesn't work?

Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

Well you could do something like his example, however in here

var sites = {{ json_encode($object) }};

you should be using $sites instead of $object

Last updated 1 year ago.
0

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!

Last updated 1 year ago.
0

So just loop through that array as you did before with foreach. use $siteatt['lat'] instead of $siteatt->lat if its not ojbect

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

Sorry, didn't have time to answer you but I'm glad that @mirago could help you :).

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

rowanwins rowanwins Joined 14 Sep 2014

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.

© 2024 Laravel.io - All rights reserved.