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
Related
Hi everybody here I have 2 paths so I return 2 arrays of tasks I need to count each array So in last I get
[6, 4]
#foreach ($path->pathtags as $Tag)
#foreach ($Tag->Tasks as $Task)
#if (!in_array($Task->id,$a))
<li class="list-group-item"> Task : {{ $Task->task_name }} </li>
#endif
#endforeach
#endforeach
Easiest php way of counting arrays is count($array) so if you want to make a new array with the 2 array counts you can do it like this:
$counts = [count($array1), count($array2)];
But if you need to count the records in the database, you will need to change the query instead of ->get() you will need to use ->count().
You should try this:
$path = count($path->pathtags);
$task = count($Tag->Tasks);
Updated answer
$path = count($path->pathtags);
#foreach ($path->pathtags as $Tag)
$task = count($Tag->Tasks);
#foreach ($Tag->Tasks as $Task)
<li class="list-group-item"> Task : {{ $Task->task_name }} </li>
#endforeach
#endforeach
I try like this :
<div class="media-body">
#foreach($categories as $category)
#php $category[] = $category->name #endphp
#endforeach
{{ implode(",", $category) }}
</div>
If the code executed, there exist error :
undefine variable category
How can I solve it?
You can simply use Laravel Collection
{{ $categories->pluck('name')->implode(', ') }}
Or if you wanna do this in foreach then
#php ($names = [])
#foreach ($categories as $category)
#php ($names[] = $category->name)
#endforeach
{{ implode(', ', $names) }}
You have to declare an array within <?php ... ?> block and then use the same in a {{blade}} block.
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]
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.
I am working on a web application using laravel 5.3, I got one problem.
I want to loop through this array.
What I tried is:
Controller:
public function postSearch(Request $request)
{
$categoryid = $request->category_id;
$locationid = $request->location_id;
$category = Category::where('id', $categoryid)->first();
$cities = Location::where('id', $locationid)->pluck('city');
$cityname = $cities[0];
$alllocations = [];
$ratecards = [];
$locations = [];
$father = [];
$i = 0;
$filteredlocations = $category->locations->where('city', $cityname)->all();
foreach($filteredlocations as $location){
$alllocations[$i] = $location->getaddetails;
$ratecards[$i] = $location->ratecard;
$i++;
}
$father[0] = $alllocations;
$father[1] = $ratecards;
return view('ads', compact('father'));
}
View is here:
#foreach($father as $key => $f)
#foreach($f as $key => $arr)
#if (isset($arr->adName))
<div class="post">
{{Html::image($arr->path,'Ad Picture',array('class' => 'ad-image'))}}
<h2 class="post-title">{{ $arr->adName }}</h2>
<p class="description">{{ $arr->description }} </p>
<div class="post-footer">
<span class="categories">
<ul class="cats">
<li class="btn btn-default">Price :
{{ $f[$key]->monthly_rate }}
</li>
<li class="btn btn-default">Status:
#if($arr->status == 1)
<p>Active</p>
#else
<p>Not-Active</p>
#endif
</li>
<li>view details</li>
</ul>
</span>
</div>
</div>
#endif
#endforeach
#endforeach
Problem:
Okay problem is when I try to get {{ $f[$key]->monthly_rate }} which is the 2nd array i.e RateCards I get nothing. I want to get the Rate Card array and from that array mothly_rate which is a field in there.
Thanks
PS : {{ $f[$key]->monthly_rate }} this return nothings in view.
The problem is that $f[$key] is an array of two objects.
That means that the properties youre looking for are inside each of those items, like this:
$f[$key][0]->monthly_rate
$f[$key][1]->monthly_rate
You will have to loop those objects to get their properties values.