Access data from a json array - php

I have this type of json object coming from a curl endpoint response:
[{"user":{"id":121153519,"full_name":"amresh12","email":null,"login":"amresh","phone":null,"website":null,"created_at":"2020-08-18T04:43:29Z","updated_at":"2020-08-25T04:04:21Z","last_request_at":"2020-08-25T04:04:22Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"age_over16":false,"parents_contacts":"","user_tags":null}},{"user":{"id":121153974,"full_name":"ritu","email":null,"login":"ritu","phone":null,"website":null,"created_at":"2020-08-18T04:49:17Z","updated_at":"2020-09-07T01:03:24Z","last_request_at":"2020-09-07T01:03:25Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"age_over16":false,"parents_contacts":"","user_tags":null}},{"user":{"id":121198739,"full_name":"abc","email":null,"login":"abc","phone":null,"website":null,"created_at":"2020-08-18T13:48:52Z","updated_at":"2020-08-30T05:26:12Z","last_request_at":"2020-08-30T05:26:57Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"age_over16":false,"parents_contacts":"","user_tags":null}},{"user":{"id":121199492,"full_name":"Sleek","email":null,"login":"support#justsleek.co.za","phone":null,"website":null,"created_at":"2020-08-18T13:59:26Z","updated_at":"2020-08-30T05:31:37Z","last_request_at":"2020-08-30T05:31:38Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"age_over16":false,"parents_contacts":"","user_tags":null}},{"user":{"id":121199660,"full_name":"Oupa","email":null,"login":"info#justsleek.co.za","phone":null,"website":null,"created_at":"2020-08-18T14:01:53Z","updated_at":"2020-08-18T15:49:51Z","last_request_at":"2020-08-18T15:49:51Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"age_over16":false,"parents_contacts":"","user_tags":null}},{"user":{"id":121234544,"full_name":"J007","email":null,"login":"sumit","phone":null,"website":null,"created_at":"2020-08-18T18:34:07Z","updated_at":"2020-08-18T18:49:32Z","last_request_at":"2020-08-18T18:49:32Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"age_over16":false,"parents_contacts":"","user_tags":null}},{"user":{"id":121712920,"full_name":"Generic User Master","email":"user#sleek.io","login":"user#sleek.io","phone":null,"website":null,"created_at":"2020-08-28T03:34:19Z","updated_at":"2020-10-01T13:28:37Z","last_request_at":"2020-10-01T13:28:34Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"age_over16":false,"parents_contacts":"","user_tags":null}},{"user":{"id":121729914,"full_name":"Service Provider Master","email":"serviceprovider#sleek.io","login":"serviceprovider#sleek.io","phone":null,"website":null,"created_at":"2020-08-28T17:24:17Z","updated_at":"2020-09-30T18:16:12Z","last_request_at":"2020-09-30T18:16:10Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"age_over16":false,"parents_contacts":"","user_tags":null}},{"user":{"id":121793428,"full_name":"a Six","email":"andile#justsleek.co.za","login":"andile#justsleek.co.za","phone":null,"website":null,"created_at":"2020-08-30T17:40:06Z","updated_at":"2020-09-07T17:31:50Z","last_request_at":"2020-09-07T17:31:50Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"age_over16":false,"parents_contacts":"","user_tags":null}},{"user":{"id":121821276,"full_name":"Oupa S.P","email":"oupa#sleek.io","login":"oupa#sleek.io","phone":null,"website":null,"created_at":"2020-08-31T15:28:40Z","updated_at":"2020-09-02T05:56:17Z","last_request_at":"2020-09-02T05:56:34Z","external_user_id":null,"facebook_id":null,"twitter_id":null,"blob_id":null,"custom_data":null,"age_over16":false,"parents_contacts":"","user_tags":null}}]
i want to access full_name from that response and display it on my index.blade.php view page as a list of name like this:
#foreach($users as $user)
<div class="chat_list">
<div class="chat_people">
<div class="chat_ib">
<h5>{{$user->full_name }}</h5>
</div>
</div>
</div>
#endforeach
Please assist me to access the value from that array above.

You can do like this :
#php
$users = json_decode($users, true);
#endphp
#foreach($users as $key => $val)
<div class="chat_list">
<div class="chat_people">
<div class="chat_ib">
<h5>{{ $val['user']['full_name'] }}</h5>
</div>
</div>
</div>
#endforeach
Above code tested here

#foreach($users as $user)
<div class="chat_list">
<div class="chat_people">
<div class="chat_ib">
<h5>{{$user->user->full_name }}</h5>
</div>
</div>
</div>
#endforeach

Related

how could I use 2 foreach in Laravel using 1 view

<h2 class="section-subheading text-muted text-center">الكورس الاول</h2>
<div class="row text-center">
#foreach ($data as $item)
<div class="col-md-4 px-3">
<div class="card shadow p-4">
<h5 class="text-center">{{$item->name}}</h5>
<hr>
show lectures
</div>
</div>
#endforeach
<h2 class="section-subheading text-muted text-center">الكورس الثاني</h2>
#foreach ($data2 as $item)
<div class="col-md-4 px-3">
<div class="card shadow p-4">
<h5 class="text-center">{{$item->name}}</h5>
<hr>
show lectures
</div>
</div>
#endforeach
</div>
If I'm understanding your question correctly.
You can nest #foreach statements like so
#foreach ($list1 as $item1)
...
#foreach ($list2 as $item2)
...
#endforeach
...
#endforeach
You would obviously want to use better names for the variables than in my sample. You do not need to have additional statements between the loops. The "..." is there to show you can.
#foreach ($data as $item)
{{ $item->name }}
#foreach ($data2 as $item2)
{{ $item2->name }}
#endforeach
#endforeach

laravel 5.6 with guzzlehttp 6 trying to get property of a title of a non object

i was trying to consume an external api. https://yts.am/api/v2/list_movies.json,
and below is my controller code.
public function listMovies()
{
$client = new Client([
'base_uri' => 'https://yts.am/api/v2/',
]);
$response = $client->request('GET', 'list_movies.json');
//dd($response);
$movies = json_decode($response->getBody()->getContents());
return view('movies.index', compact('movies'));
}
and this is my view file
<div class="container">
<div class="row py-5">
<div class="col">
#foreach($movies as $movie)
<div class="card">
<div class="card-header">
{{$movie->title }}
</div>
<div class="card-body">
{{ $movie->summary }}
</div>
</div>
#endforeach
</div>
</div>
</div>
but iam getting this error: Trying to get property 'title' of non-object (View:
It's because your $movies object don't have property title and summary, but you can access it like this:
#foreach($movies->data->movies as $movie)
<div class="card">
<div class="card-header">
{{$movie->title }}
</div>
<div class="card-body">
{{ $movie->summary }}
</div>
</div>
#endforeach

How to link comments to each post by id with Laravel Blade

I have created a Post and Comment application. I am trying to link the comments to the id of the post. I am trying to use PHP in a Blade template to do this. I get the error that the variable post_id does not exist. I want to use the #if and #foreach of Blade. The problem seems to be in the #if statement.
Here is my HTML:
<div class="col-md-9">
<div class="row">
#foreach($posts as $post)
<div class="col-md-12 post" id="post{{ $post->id }}">
<div class="row">
<div class="col-md-12">
<h4>
{{ $post->title }}
</h4>
</div>
</div>
<div class="row">
<div class="col-md-12 post-header-line">
<span class="glyphicon glyphicon-user"></span> by {{ $post->user->firstName }} {{ $post->user->lastName }} | <span class="glyphicon glyphicon-calendar">
</span> {{ $post->created_at }} | <span class="glyphicon glyphicon-comment"></span><a href="#">
3 Comments</a>
</div>
</div>
<div class="row post-content">
<div class="col-md-2">
<a href="#">
<img src="/images/random/postimg.jpg" alt="" class="img-responsive postImg">
</a>
</div>
<div class="col-md-10">
<p>
{{ $post->post }}
</p>
</div>
</div>
<div class="row add-comment">
<div class="form-group">
<input type="text" name="addComment" placeholder="Add your comment" class="form-control" v-model="comment">
</div>
<input id="addComment" type="submit" name="submitComment" value="Submit Comment" class="btn btn-default" v-on:click="submitComment" data-id="{{ $post->id }}">
</div>
#if($post->comment->post_id == $post->id)
#foreach($post->comment as $comment)
<div class="row">
<div class="col-md-12 mb-r">
<div class="card example-1 scrollbar-ripe-malinka">
<div class="card-body">
<h4 id="section1"><strong>By: {{ $comment->user->firstName }} {{ $comment->user->lastName }}</strong></h4>
<p>{{ $comment->comment }}</p>
</div>
</div>
</div>
</div>
#endforeach
#endif
</div>
#endforeach
</div>
</div>
Here is my PostController:
public function index(){
$posts = Post::with('comment', 'user')->orderBy('id', 'desc')->limit(20)->get();
return view('post.posts')->with('posts', $posts);
}
You don't need #if statement, remove it. $post->comment is a collection, that 's why your are not getting post_id. if you want to check, then check inside foreach.
#foreach($post->comment as $comment)
<div class="row">
<div class="col-md-12 mb-r">
<div class="card example-1 scrollbar-ripe-malinka">
<div class="card-body">
<h4 id="section1"><strong>By: {{ $comment->user->firstName }} {{ $comment->user->lastName }}</strong></h4>
<p>{{ $comment->comment }}</p>
</div>
</div>
</div>
</div>
#endforeach
You need to add post_id to the comments table and use the relationship:
public function comments()
{
return $this->hasMany(Comment::class);
}
Then you'll be able to load related comments:
Post::with('comments', ....
And iterate over posts and their comments:
#foreach ($posts as $post)
#foreach ($post->comments as $comment)
{{ $comment->content }}
#endforeach
#endforeach
I think if you create relationship between Post model and Comment model (post table and comments table), your #if is useless. I recommend you to check your corresponding models.
Your code should be like this,
Post.php
public function comments(){
return $this->morphMany('App\Comments','commentable');
}
Comment.php
public function commentable(){
return $this->morphTo();
}
in my app Comment model is shared by some other models also.
Eg :- User’s Questions also have comments.
Find more details in laravel:Eloquent page

Blade Template Layout Structure

I need to create a row for each 3 attachments. Something like
#foreach($email->attachment as $attach)
<div class="row">
<div class="col-lg-4"></div>
<div class="col-lg-4"></div>
<div class="col-lg-4"></div>
</div>
#endforeach
<div class="row">
<div class="col-lg-4">1st attachment</div>
<div class="col-lg-4">2nd attachment</div>
<div class="col-lg-4">3rd attachment</div>
</div>
<div class="row">
<div class="col-lg-4">4th attachment</div>
<div class="col-lg-4">5th attachment</div>
<div class="col-lg-4">6th attachment</div>
</div>
and so on! I had searched for the question but just found This link but It's actually different from my perspective.
Thanks
Use array_chunk - it is designed for this each purpose - so you can cut an array into sub-sizes and loop through each group
#foreach (array_chunk($email->attachment->toArray(), 3, true) as $array)
<div class="row">
#foreach($array as $attachment)
<div class="col-lg-4">{{ $attachment['id'] }}</div>
#endforeach
</div>
#endforeach
And using #Lukasgeiter's comment - you could also do it this way if it is a Laravel Collection
#foreach ($email->attachment->chunk(3) as $array)
<div class="row">
#foreach($array as $attachment)
<div class="col-lg-4">{{ $attachment->id }}</div>
#endforeach
</div>
#endforeach
Try this, it's faster than generate 2 arrays:
<div class="row">
#foreach( $email->attachment as $index => $attach)
<div class="col-lg-4">{{$attach}}</div>
#if( $index+1 === 0)
</div><div class="row">
#endif
#endforeach
</div>

how to do laravel with #foreach and #if

I have seen as "there are no posts" if they are available as comment even the URL is working property.
I have discover on laravel.com there is not lack of information or example inside the #if and #else . http://laravel.com/docs/4.2/templates#other-blade-control-structures.
here is my code is
#if ($post->comments)
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#endif
#foreach ($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#endforeach
however I have tried change as #if ($post->comments as $comment) is said "syntax error, unexpected 'as' (T_AS)" do you have any idea to solve it?
or you could use the blade's #forelse loop this way
#forelse($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#empty
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#endforelse
Just a guess, but I think this is what you wanted to do
#if ($post->comments)
#foreach ($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#endforeach
#else
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#endif
You are trying to put a foreach in the condition of an if statement
Put the foreach in the entire #if, and check for NOT comments :
#if (!$post->comments)
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#else
#foreach ($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#endforeach
#endif
This should be the control flow you are looking for.

Categories