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
I don't really know the title of this one and tried searching but I don't have any idea on what keywords should I put it on so I create a new one but if there is already a similar post to this. Can you link it? My question is how do I achieve this goal in the image below.
#php $i = 0; #endphp
#foreach ($posts as $key => $post)
#if ($key % 2 === 0)
<div class="row">
<div class="col-md-4"><img src="{{ $post->image }}" /></div>
<div class="col-md-8">
- {{ $i+=1 }}
- {{ $i+=2 }}
- {{ $i+=3 }}
</div>
</div>
#else
#endif
#endforeach
The alternate class from bootstrap is working but the numbers that would increment on each value is I don't have any idea on how to get it done.
I'm not familiar with Larval syntax, but for the logic I think you should have something more like this:
#php $i = 0; #endphp
#foreach ($posts as $key => $post)
#if ($key % 2 === 0)
<div class="row">
<div class="col-md-4"><img src="{{ $post->image }}" /></div>
<div class="col-md-8">
- {{ ++$i }}
- {{ ++$i }}
- {{ ++$i }}
</div>
</div>
#else
<div class="row">
<div class="col-md-8">
- {{ ++$i }}
- {{ ++$i }}
- {{ ++$i }}
</div>
<div class="col-md-4"><img src="{{ $post->image }}" /></div>
</div>
#endif
#endforeach
Things to note that I changed:
1. Increment $i by one for EACH number, otherwise you'll be skipping numbers
2. When $key is NOT event, print the image on the other side.
Again, I'm not familiar with larval syntax, and I also don't know what the key/values of your $posts array are (if your keys aren't alternating even/odd you won't have the images back-and-forth between left-and-right).
Hopefully this gives you some direction though.
Let's say I have an array of data which I want to display as a list:
$goods = [
['title' => 'First item', 'description' => 'First item description'],
['title' => 'Second item', 'description' => 'Second item description'],
['title' => 'Third item', 'description' => 'Third item description']
];
And the following view:
<div class="good-block">
<h1>{{ $good['title'] }}</h1>
<p>{{ $good['description'] }}</p>
</div>
Now I want to display all the items using <hr> as a separator.
Therefore, the final view is as follows:
#foreach ($goods as $good)
<div class="good-block">
<h1>{{ $good['title'] }}</h1>
<p>{{ $good['description'] }}</p>
</div>
<hr />
#endforeach
The problem is that, obviously, the separator will be displayed after the last item, not only between the elements. In the case of simple arrays implode can be used as a tool for splicing.
But how to use this approach in the template files? Is there a different approach, allowing to keep the code clean and logical, without adding unnecessary conditions like checking the current element is not last element of an array?
Thanks!
A simple trick is to keep a $counter, like so:
#$counter = 0;
#foreach ($goods as $good)
#if ( $counter > 0 ) print '<hr />';
<div class="good-block">
<h1>{{ $good['title'] }}</h1>
<p>{{ $good['description'] }}</p>
</div>
#$counter++
#endforeach
Now your hr will only appear before your entry, but it skips the first one.
You can do this in Blade:
https://laravel.com/docs/5.3/blade#loops
#foreach ($goods as $good)
<div class="good-block">
<h1>{{ $good['title'] }}</h1>
<p>{{ $good['description'] }}</p>
</div>
#if (!$loop->last)
<hr />
#endif
#endforeach
however, if you're not using blade, you can use a key variable in the foreach loop like this:
#foreach ($goods as $index => $good)
<div class="good-block">
<h1>{{ $good['title'] }}</h1>
<p>{{ $good['description'] }}</p>
</div>
#if ($index < count($goods) - 1)
<hr />
#endif
#endforeach
I want to display default data if the table is empty. I searched in the laravel documentation and found this.
In my controller i send data to my show.blade.php
public function show($id)
{
//Get profile data from the user
$user = User::find($id);
return view( 'gebruiker/show',[
'user' => $user,
] );
}
And in my blade I display the data
#foreach ($user->profile as $profile)
<div class="col-md-12 col-xs-12" align="center">
<h1>{{ $profile->user->name or 'Geen naam' }}
</h1>
<h3>{{ $profile->age or 'Geen leeftijd' }}</h3>
<span>{{ $profile->country or 'Geen land' }}</span>
</div>
<div class="col-md-6 col-xs-6 follow line" align="center">
<h3>
Bezoekers <br/> <span>{{ $profile->visitors or 'Geen bezoekers' }}</span>
</h3>
</div>
<div class="col-md-12 col-xs-12 login_control text-center">
<br>
{{ $profile->profile or 'Geen beschrijving' }}
</div>
#endforeach
When $profile->age is not empty it shows data but when there is no data it just shows nothing. What am I doing wrong?
In blade "or" checks only if variable exists.
Lest try something like that:
#if(!empty($profile->age))
{{ $profile->age }}
#else
Geen leeftijd
#endif
You can also try this:
#if($profile->age !='' && $profile->age !='0')
{{ $profile->age }}
#else
Geen leeftijd
#endif
Depending on your needs you can create an accessor in your model:
public function getAgeAttribute($value)
{
return !empty($value) ? $value : 'Geen leeftijd';
}
Ideally this should live in a presenter. Too bad {{ $profile->age or 'Geen leeftijd' }} doesn't work, it should. Probably someone will do a PR for it soon enough.
I recently created a commenting system for a blog. When there is one post in the blog, the commenting sytem works fine. However, when there are two posts on the blog, and I comment on the FIRST post, the comment shows up on the SECOND post. Similarily, if I add another post, so there are three posts in my blog, when I comment on the first post, the comment shows up under the third post. In the same situation, when I comment on the second post, the comment shows up under the third post. Here is my code:
/index.blade.php
#section('content')
<div class="col-md-8 col-md-offset-2">
<hr>
#foreach($posts as $post)
<h3>{{ $post->title}}</h3>
<p>by {{ $post->user->name }}</p>
<img src='{{ asset($post->image_path) }}' class="img-responsive" id="blogpic"/>
<p>{{ Str::words($post->body) }}</p>
<p>Read More...</p>
<hr>
{{ Form::open(array('action' => 'CommentController#newComment')) }}
{{ Form::hidden('post_id', $post->id) }}
<div class="row">
<div class="col-md-10">
{{ Form::text('body', null, array('class'=>'form-control')) }}
</div>
<div class="col-md-2">
{{ Form::submit('Post', array('class'=>'btn btn-info')) }}
</div>
</div>
<br>
#foreach($post->comments as $comment)
<div class="comment">
<p><span class="bold">{{ $comment->user->name }}</span>: {{ $comment->body }}</p>
</div>
#endforeach
#endforeach
</div>
#stop
Here is my new comment controller:
public function newComment()
{
$comment = New Comment();
$comment->user_id = Auth::user()->id;
$comment->post_id = $post_id = Input::get('post_id');
$comment->body = Input::get('body');
Post::find($post_id)->comments()->save($comment);
return Redirect::action('BlogController#index');
}
I am pretty sure the problem is in my index file, and I can provide more code if needed. My comment only seems to get the most recent id in this line
{{ Form::hidden('post_id', $post->id) }}
What could be going wrong?
Thanks in advance