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.
Related
I know this is relatively simple but it's driving me absolutely insane!
a little background, my app takes an XML feed to parse, this has nested fields such as features
it's in my DB, as an obj/array, with 'feature' as the key.
now my for each loop is displaying all this data in one UL.
Code Below.
Any Help Greatly Appreciated
//model
public static function getFeatures($id){
$featuresArray = [];
$featuresquery = DB::table('properties')
->selectRaw("features")
->where('id', $id)
->get('');
$k = $featuresquery;
foreach ($k as $feat):
if (is_array($feat)) {
$featuresArray[] = (array_key_exists('features', $feat)) ? $feat['features'] : '';
}
return $feat;
endforeach;
return $featuresArray;
}
//Controller
public function details($id) {
$property = Property::findorFail($id);
$agent = Agent::where("id",$property->agent_id)
->with([
'ads'
])->first();
$template = Agent::getTemplateName($agent);
$banners = Banner::getBanners($agent->id);
$areaAndLocations = Property::getAreaAndLocations($agent->id);
$property_type = Property::getPropertyTypes($agent->id);
$query_params = Property::getQueryParams();
$features = Property::getFeatures($property->id);
return view('frontend.'.$template.'.property.details', ['template' => $template, 'banners' => $banners, 'areaAndLocations' => $areaAndLocations, 'property_type' => $property_type, 'query_params' => $query_params, 'features' => $features])
->withDetails($property)
->withAgent($agent);
}
//View
<div class="features">
#foreach($features as $feature)
<ul><li>
{!! json_encode(json_decode($feature)) !!}
</li></ul>
#endforeach
</div>
because you giving ul>li in foreach which generating ul with every iterate,
follow the below code
<div class="features">
<ul>
#foreach($features as $feature)
<li>
{!! json_encode(json_decode($feature)) !!}
</li>
#endforeach
</ul>
</div>
Move the <ul> and </ul> tags to outside the for loop. Your code is generating a new <ul>...</ul> pair for every item on the list.
Change the foreach loop like this
<div class="features">
<ul>
#foreach($features as $feature)
<li>
{!! json_encode(json_decode($feature)) !!}
</li>
#endforeach
</ul>
</div>
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 want to use $news_data variable in my span tag that is under foreach loop.
this is my code..
#foreach($news as $key => $value)
<li>
<h3 class="title">{{$value->title}}</h3>
<div class="description">
<div class="popup">
<i class="fa fa-newspaper-o"></i>
<span class="popuptext" id="myPopup"> **use $news_data here**</span>
</div>
{{$value->news_date}} : {{$value->description}}...
</div>
</li>
#endforeach
this is my controller function:
public function index()
{
$news = DB::table('feed_news')
->select(DB::raw('news_date,title,LEFT(`description`, 40) as
`description`'))
->orderBy('news_date','DESC')->get();
$news_data = DB::table('feed_news')->select(DB::raw('description'))->get();
return view('DisplayNews.index')
->with('news_data',$news_data)
->with('news',$news);
}
my $news & $news_data comes from controller.
what is the best way to do this..?
You can use different variables inside for loop, that doesn't belong to loop.
Just use that variable as any other variable:
Replace **use $news_data here** with
{{$news_data}}
You are getting a collection again in $news_data , hence you will have to loop it again to get it's description.
for example:
foreach($newsdata as $news)
{
<span>{{$news->description}}</span>
}
<span class="popuptext" id="myPopup">{{ json_encode($news_data) }}</span>
Above code will show all data of that $news_data
If you want to detail showing instead of showing all. Loop it again using #foreach
To Show All
#foreach ($news_data as $newsDesc)
<span class="popuptext" id="myPopup">{{ $newsDesc->description }}</span>
#endforeach
I wanted to update multiple rows in the database and have found a way to do it. but it does not seem like the best way to do it, since it is doing it in multiple calls at the moment.
I wanted to now if there is a better way to do this.
The homecontroller with the updatePersons function:
<?php
class HomeController extends BaseController {
public function index()
{
$persons = Person::get();
return View::make('hello')
->with(compact('persons'));
}
public function updatePersons()
{
$persons = Person::get();
foreach ($persons as $person) {
$person->fname = Input::get('fname'.$person->id);
$person->lname = Input::get('lname'.$person->id);
$person->save();
}
return Redirect::route('home')->with('succes', 'Siden er opdateret');
}
}
the view with the form
#extends('layouts.master')
#section('content')
<div class="container">
<ul class="list-group">
{{ Form::open(array('route'=>'personUpdate', 'method' => 'post')) }}
#foreach ($persons as $person)
<li class="list-group-item">
{{ Form::text('fname'.$person->id,$person->fname,array('class'=>'form-control', 'placeholder'=>'fname')) }}
{{ Form::text('lname'.$person->id,$person->lname,array('class'=>'form-control', 'placeholder'=>'lname')) }}
</li>
#endforeach
<li class="list-group-item">
<div class="box-footer">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</li>
{{ Form::close() }}
</ul>
</div>
</div>
#stop
The routes:
<?php
Route::get('/', array('as'=>'home', 'uses'=>'HomeController#index'));
Route::post('/', array('as'=>'personUpdate', 'uses'=>'HomeController#updatePersons'));
I have tried to use the savemany() function on $persons after the foreach loop in updatePersons() but with no results.
If you're updating many rows with each with different values, there's no easier way to do it.
Could you think of how you'd write this in SQL?
My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
I think you are closing the PHP tag in the wrong place. Change to this:
<?php print_r($count);
$data = $this->BasicController->count {{ ($row->sub_id) }}
echo $data['count'];//here is the rightplace to close your PHP ?>
</span></div>
All PHP code needs to stay between the <?php and ?>
Looking to your image, apparently, it's printing the $count and then it's printing the plain text , so you have 80$data = ...
If you counted your rows in controller by the code below
$count = DB::table('products')->where('sub_id', $id)->count();
then why wrote so many code in html for counting?
Remove all code form counting form your html and just use this {{ $count }}