trying to get darksky api data in laravel - laravel

Hi I connected to a third party api and trying to get the data looping through the object but I keep getting errors like the data is not there
Please see my controller and view below
Controller
function weather() {
$client = new Client();
$req = $client->get('https://api.darksky.net/forecast/6c314524bbb5e8ea74d27ea433a64a19/37.8267,-122.4233');
$response = $req->getBody()->getContents();
$data = json_decode($response, true);
return view('weather', compact('data'));
}
Blade View
#foreach($data as $r)
<span>{{ $r->latitude }}</span>
#endforeach
And this is the error
Trying to get property 'latitude' of non-object
What am I doing wrong please?
I mean the data is there, see pic
enter image description here

$data is an array, access the latitudeĀ as a key in your view
<span>{{ $data['latitude'] }}</span>
Hope this helps

Related

Laravel 5 (custom) method for calculating age(in days) does not exist

there's a lot of similar questions but I did not find the solution for my problem, so here's the issue:
I have a simple function that should be used for calculating age (in days), listed in the SubmissionController:
public function agingDate($sub) {
$to = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $sub);
$now = Carbon::now();
$diff_in_days->age = $to->diffInDays($now);
return $diff_in_days;
}
Also, in the same controller:
public function index() {
$submission = Submission::orderBy('location_address_state', 'asc')
->get();
return view('/subs/index', [
'submission' => $submission
]);
}
And finally, in my view, I'm trying to call this function:
#if($submission)
#foreach($submission as $sub)
<p>{{ $sub->created_at->agingDate() }}</p>
#endforeach
#endif
The message I'm receiving is "Method agingDate does not exist. (View: /subs/index"). Why can't I access this function - it's in the same controller?
In order to keep the separation of concerns intact, I would suggest that you move your agingDate() method into your submission model.
So, in your Submission model, you could refactor it in the following way:
public function agingDate() {
$to = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $this->created_at);
$now = Carbon::now();
return $to->diffInDays($now);
}
In your blade file, you should then be able to call:
#if($submission)
#foreach($submission as $sub)
<p>{{ $sub->agingDate() }}</p>
#endforeach
#endif
The approach you are currently using doesn't work, since you are trying to call the aging method on a plain property of your Submission instance.

Laravel: Why isn't the query result showing in my view?

I am successfully getting results in/from my controller like this:
$session_id and $user_id are getting passed into my method.
Controller
// get all sets belonging to the user for the given session
$session = Session::findOrFail($session_id);
$sets = Set::where('session_id', $session_id)->where('user_id', $user_id);
$user = User::findOrFail($user_id);
return View('users.index')
->with('session', $session)
->with('sets', $sets)
->with('user', $user);
View
<p>{{ $user->first_name }} has {{ $sets->count() }} existing set{{ ($sets->count() > 1 ? 's': '') }}.</p>
<ul class="list-unstyled">
#foreach ($sets as $set)
<li>
Set
</li>
#endforeach
</ul>
I am seeing "Foo has 1 existing set." and I can see the record in the DB. However, I'm not getting into the loop at all. If I add/remove records, my paragraph text updates accordingly as well - but I still never get into the loop to show each set. I'm sure it's something obvious, but I sure don't see it.
Thank you for any suggestions!
Change the query of sets to this:
$sets = Set::where('session_id', $session_id)->where('user_id', $user_id)->get();
As get() method will return you the collection object.

Pass sql result from controller to views in laravel

I'm having a problem to get my data from controller into my view, and I was hoping someone could help me.
Here is the code for my controller:
$Info = new Info();
$Info = DB::select('SELECT * FROM 'tblinfo');
and I only echo it in controller because my value results doesn't pass into the view, here's the additional code for my controller:
foreach ($Info as $Infos)
{
echo $Infos->fname;
echo $Infos->gender;
}
$data['result'] = $Infos->fname;
$data['result'] = $Infos->gender;
$this->view = View::make(App::make('web')->makeDeviceVersionPath('.').'.infofolder.infofile', $data);
Here is the code for my views:
#foreach $result as $info
{{ $info->name }}
{{ $info->gender }}
#endforeach
I hope someone could help me to pass my sql result to my views. Thank you! :)
if u have model u dont need to type raw sql code
just do like this
$info= Info::all();
return View('Index.index')->with('info',$info); // use ->with('*',$*) to send data to view
// Index is View folder
// .index is view name
Now in view grab $info data with for each
#foreach($info as $infodata)
<h3>{{ $infodata->name }}</h3>
<h3>{{ $infodata->gender}}</h3>
#endforeach
Adnan's answer is ok.
Op you should really check Eloquent and Response documentation from laravels website.
http://laravel.com/docs/4.2/eloquent
http://laravel.com/docs/4.2/responses

How do i retrieve the content data to blade from this json

i'm encountered with an blade issue.
[{"banner_type":"2","content":"[\"e6aaaff3ae42af3af3a9cb6a45845da8f905c906Screenshotfrom2015-01-1917:17:31.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2115:16:01.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2208:49:53.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2212:36:42.png\"]"}]
This is the json i'm getting from webservice, i need to get each image url and need to assign to a tag . how could i achive it?
#foreach($json->content as $img)
<img src="{{$img}}" alt="alt text">
#endforeach
From the information provided that is the most I can give you.
For the given json, you can try something like this on your controller:
public function index()
{
$json_string = '[{"banner_type":"2","content":"[\"e6aaaff3ae42af3af3a9cb6a45845da8f905c906Screenshotfrom2015-01-1917:17:31.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2115:16:01.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2208:49:53.png\",\"559a0e13473a22ac5bde041c5260257a12d1811eScreenshotfrom2015-01-2212:36:42.png\"]"}]';
$json = json_decode($json_string, true);
$images = json_decode($json[0]['content']);
return view('index', compact('images'));
}
Then, on your blade view:
#foreach($images as $image)
<img src="$image">
#endforeach

Laravel explain me how working query

I have a problem with query on laravel.
Please show me how it work, because I can't understand doc.
For example, I have VideoController.php and I have some data from forms:
$gall = array(
'name' => Input::get('name'),
'user_id' => Auth::id()
);
now I want to add this data to DB, but I don't know how to call to create function in model (and how this function should look).
And please explain me, how I should select data from database and display it on view, where for example user_id = 15;
In your VideoController.php file you should have a method to store the data, which you should post your data to through a form or something.
In a view, this form should look something like this:
{{ Form::open(['route' => 'video.store']) }} <!-- check your routes to see if video.store exists, you'll get an error otherwise -->
... form elements ...
{{ Form::submit('Submit') }}
{{ Form::close() }}
In your store() function you should have code similar to this
$video = new Video(); // if your video model is Video.php
$video->name = Input::get('name');
$video->user_id = Auth::id();
$video->save();
And if you want to display data, i.e. your video index or something, in your VideoController.php file, in the index() function:
$videos = Video::all();
return View::make('video.index', array('videos' => $videos));
then in your view
#foreach($videos as $video)
{{ $video->name }}
#endforeach

Resources