I am getting the result like follow from model with one-to-many relationship
{
"id":1,
"name":"asdjash",
"position":"jhkjh",
"about":"hkjhkj",
"message":"hkjhkj",
"image":"demo.jpg",
"status":"Active",
"created_at":"2017-02-07 00:00:00",
"updated_at":"2017-02-09 00:00:00",
"personality_slider_images":[
{
"id":1,
"image_name":"one.jpg",
"personality_id":"1",
"created_at":"2017-02-09 00:00:00",
"updated_at":"2017-02-11 00:00:00"
},
{
"id":2,
"image_name":"two.jpg",
"personality_id":"1",
"created_at":"2017-02-10 00:00:00",
"updated_at":"2017-02-26 00:00:00"
}
]
}
I want to display on page all the details
as follow
<div class="well">
<p><h4>Name : </h4>
{{ $personality->name }}
</p>
<p><h4>Position : </h4>
{{ $personality->position }}
</p>
<p><h4>About : </h4>
{{ $personality->about }}
</p>
<p><h4>Image : </h4>
<img src="{{ URL::to('/contents/personality/' . $personality->image) }}" alt="{{ $personality->image }}" width="150" height="180" style="display: block; ">
</p>
<p><h4>Message : </h4>
{{ $personality->message }}
</p>
<p><h4>SliderImage : </h4>
{{ $personality }}
</p>
<p><h4>Status : </h4>
{{ $personality->status }}
</p>
</div>
I have tried this
#foreach($personality->personality_slider_images as $p)
{{ $p->image_name }}
#endforeach
But it gives error as follow
Invalid argument supplied for foreach()
Can anyone please help me solve this problem ?
I see you have got json array,
Decode it using json_decode
$decode = json_decode($encode);
Now traverse your loop as,
#foreach($personality->personality_slider_images as $p)
{{ $p->image_name }}
#endforeach
It will work.
Give it a try.
You should add a for each for your actual array, not for a variable in that array.
Assuming your array name is $personalities, it should look like this.
#foreach ($personalities as $personality)
Insert the HTML from your sample above in here.
#endforeach
To retrieve the image object you would have to do something like the following
$personality->personality_slider_name['image_name]
Related
I have a dataset like below inside a cell named rolls.
[
"142650",
"142651",
"142603",
"142604"
]
I have an array named $rooms, where each room has cell called rolls.
#foreach($rooms as $room)
{{ $room->rolls }}
#endforeach
The code above displaying data like below in view
["142650", "142651", "142603", "142604", "142605"]
But I want to display like below...
142650, 142651, 142603, 142604, 142605
I have tried this
#foreach($rooms as $room)
#foreach($room->rolls as $roll)
{{ $roll }}
#endforeach
#endforeach
But getting error like below
Invalid argument supplied for foreach()
I think the cleanest solution, without changing controller is
#foreach($rooms as $room)
<h1>{{ $room->name }}</h1>
#foreach(json_decode($room->rolls) as $roll)
{{ $roll }}
#endforeach
#endforeach
Try not to use #php or <?php ?> in your blade templates
This is the solution I have found, I have not changed anything in my controller, this is what I have done in the view.
#foreach($rooms as $room)
<h1>{{ $room->name }}</h1>
<?php $rolls = json_decode($room->rolls); ?>
#foreach($rolls as $roll)
{{ $roll }}#if(!$loop->last), #endif
#endforeach
#endforeach
What is the correct way to call the "Links" function after this "Foreach"?
I don't know how to handle the variable to put in function.
#inject('usuarios', 'App\User')
#foreach($usuarios->getIndicados() as $user)
#endforeach
<div class="row">
<div class="col-12 text-center">
{{ $usuarios->getIndicados()->links() }}
</div>
</div>
Maybe it's just an editing error, but in your output the -tags don't seem to be closed again. Also, there should be no space like < a> at the beginning of the tag. And < a hr_ef= ... is obviously wrong.
In order to style them, you can add a class attribute to the tags while building the string and do the style-stuff in css.
This is what laravel document provides. You need to add links in the collection.
<div class="container">
#foreach ($users as $user)
{{ $user->name }}
#endforeach
{{ $users->links() }}
I'm in a WP project. I created a custom post type called Team Members
I also created a custom field block with a field called contact_members which is a repeater and has two sub-fields called location_map and member. This member sub-field is related with the custom post type team member.
I have no issues with getting the location_map.
The issue is that I can't get the post type fields. (coming from contact_members->member->post type
<?php
$members = get_field('contact_members');
?>
#foreach($members as $member)
<div class="member {{ $member['location_map'] }}">
<img class="map" src="{{ get_the_post_thumbnail_url($member['member']->ID) }}">
<h3>{{ get_the_title($member['member']->ID) }}</h3>
<p class="position">{{ $member['member']->position }}</p>
<p class="location">{{ $member['member']->location }}</p>
Contact
</div>
#endforeach
On a first look i cant see any problems with your code. What i consider the most possible senario is ( since that i guess you are using the ACF plugin ) the $member['meber'] variable holds the (int) post_id and not the Post Object. Have you tried var_dump() ?
Cheers!
I found the answer, I should put [0] after $member['member'], in order to reach the array's first element and after that I can finally go after what I want from the object, so:
<?php $members = get_field('contact_members'); ?>
#foreach($members as $member)
<div class="member {{ $member['location_map'] }}">
<img class="member-image" src="{{ get_the_post_thumbnail_url($member['member'][0]->ID) }}">
<h3 class="member-name">{{ $member['member'][0]->post_title }}</h3>
<p class="position">{{ $member['member'][0]->position }}</p>
<p class="location">{{ $member['member'][0]->location }}</p>
Contact
</div>
#endforeach
So, this is my json format data
{
"New Purchase Orders Created":{
"date":"2018-02-26 14:05:14.000000",
"timezone_type":3,
"timezone":"UTC"
},
"colour":"bg-primary"
}
This is my view in blade.php
<ul class="list-group list-group-flush">
#foreach( $notifications as $notification )
<?php $notificationData = json_decode($notification->data, true);?>
#foreach( $notificationData as $key=>$item )
<li class="list-group-item text-white {{ $notificationData['colour'] }}">
#if ($key!='colour')
{{ $key }}<br>
{{ $item['date'] }}
#endif
</li>
#endforeach
#endforeach
</ul>
I want to get the data into the blade. So what should I do?
This is what i want to get.
Please help. Thanks.
Assuming the above JSON is an object array like this:
<?php
$notifications = '[{
"New Purchase Orders Created":{
"date":"2018-02-26 14:05:14.000000",
"timezone_type":3,
"timezone":"UTC"
},
"colour":"bg-primary"
},
{
"New Purchase Orders Created":{
"date":"2018-02-26 14:05:14.000000",
"timezone_type":3,
"timezone":"UTC"
},
"colour":"bg-primary"
}]';
?>
Thus, the script will be like this:
<ul class="list-group list-group-flush">
#foreach( json_decode($notifications, true) as $notification => $data )
<?php $item = $data[key($data)]; ?>
<li class="list-group-item text-white {{ $data['colour'] }}">
{{ key($data) }}<br>
{{ $item['date'] }}
</li>
#endforeach
</ul>
Use json_decode. This works with JSON you've shown:
{{ json_decode($json, true)['New Purchase Orders Created']['date'] }}
If New Purchase Orders Created key may be different in every JSON, use the array_first() helper:
{{ array_first(json_decode($json, true))['date'] }}
If you will have many elements, iterate over them:
$array = json_decode($json, true);
#foreach ($array as $key => $element)
{{ $array[$key]['date'] }}
#endforeach
i got error while i'm trying to fetch my data in view. This is my code :
controller
public function create()
{
$categories = Category::all();
$tag = Tag::groupBy('name')->pluck('name');
$tags = json_encode($tag);
return view('backend.articles.create', compact('categories', 'tags'));
}
view
fetch data categories
<div class="panel-body">
#foreach ($categories as $category)
<div class="checkbox">
<label>
{!! Form::checkbox('category_id[]', $category->id) !!} {{ $category->name }}
</label>
</div>
#endforeach
</div>
fetch data tags
jQuery(document).ready(function($) {
$('#tags').magicSuggest({
cls: 'form-control',
data: {!!$tags!!},
#if (count(old('tags')))
value: [
#foreach (old('tags') as $tag)
'{{$tag}}',
#endforeach
]
#endif
});
});
I dont know why i got this error message : Invalid argument supplied for foreach(). Could anybody to help me to fix this problem ?
view
<div class="panel-body">
#if($categories)
#foreach ($categories as $category)
<div class="checkbox">
<label>
{!! Form::checkbox('category_id[]', $category->id) !!} {{ $category->name }}
</label>
</div>
#endforeach
#endif
script
jQuery(document).ready(function($) {
$('#tags').magicSuggest({
cls: 'form-control',
data: {!!$tags!!},
#if (is_array(old('tags')))
value: [
#foreach (old('tags') as $tag)
'{{$tag}}',
#endforeach
]
#endif
});
});
Try to use is_array to make sure old('tags') returning array, I believe the problem because the old('tags') is not returning array.