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>
Related
I need your help. Lets say I have array with 10 elements and every record to be shown on every iteration and I want to be done with Livewire, let me share with you some code and will tell you what I have tried till now.
public $content;
public $array = ['first', 'second', 'third' ,'fourth'];
foreach ($array as $item) {
sleep(1);
$this->content[] = "Element ${item}";
}
<div class="modal-body">
#if ($content)
<ul class="listree">
#foreach ($content as $element)
<li>
<div class="listree-submenu-heading">
{!! $element['title'] !!}
</div>
<ul class="listree-submenu-items">
#foreach ($element['elements'] as $sub)
<li>
<div class="listree-submenu-heading">
{!! $sub['title'] !!}
</div>
</li>
#endforeach
</ul>
</li>
#endforeach
</ul>
#endif
</div>
My idea is display first record, wait 1 second, display first and second record, wait 1 second, display first, second and third records and so on... How I can do this with livewire. The problem is that $content is filled with the information after all iteration and then the component is refreshed.
I tried on every iteration to send custom event which will call refresh method, but without succes. I will appreaciate any advice, and if you need more information, I will provide it.
Assumming you're also using alpinejs, this can be done pretty easily.
<x-app-layout>
<div class="text-gray-800 ml-10">
<ul class="bg-green-200">
<!-- The main foreach loop. -->
#foreach (range('a', 'z') as $element)
<!-- render al <li> tags with display:none. -->
<!-- Show the 1st after 0s, the 2nd after 1s, the 3rd after 2s, ... -->
<li class="bg-blue-200 m-5"
x-data="{show: false, index: {{ $loop->index }} }"
x-init="setTimeout(() => show = true, index * 1000)">
<div x-show="show">
{{ $element }}
...
</div>
</li>
#endforeach
</ul>
</div>
</x-app-layout>
$loop is a special object you can access within a #foreach block.
New to Laravel and feel that I'm missing something important here. I am uploading and saving multiple files to a folder and saving an array of the images names to the database(simple enough). Currently saving the images name in the json_encode() format so they are formatted like so["kittyTest.jpeg","kitty_2.jpeg","kitty_3.jpeg"]. So when I try and print them out to the view I get them in the json format and am trying to display them in an array formate or in some way that I can use the filename for the source image. Any help or guidance would be much appreciated.
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Form;
use DB;
class FormController extends Controller
{
public function index()
{
$images = DB::select('select * from forms');
//dd(json_decode($images[0]->filename));
return view('index', ['images'=> $images]);
}
public function create()
{
return view('create');
}
public function store(Request $request)
{
$this->validate($request, [
'filename' => 'required',
'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
if ($request->hasFile('filename')) {
foreach ($request->file('filename') as $image) {
$name = $image->getClientOriginalName();
$image->move(public_path().'/images/', $name);
$data[] = $name;
}
}
$form = new Form();
$form->filename = json_encode($data);
$form->save();
return back()->with('success', 'Your images have been uploaded');
}
}
View
<body>
<div class="container">
<h3 class="jumbotron">Laravel Multiple File Upload</h3>
<p>Here are the images we have in the database</p>
<ul class="list-group">
#foreach ($images as $image)
<li class="list-group-item">
{{ $image->filename }}
</li>
#endforeach
</ul>
</div>
</body>
<ul class="list-group">
#foreach ($images as $image)
#php $image_array = json_decode($image->filename,true); #endphp
#foreach ($image_array as $img)
<li class="list-group-item">
{{ $img }}
</li>
#endforeach
#endforeach
</ul>
try this and let me know
In your controller use array_map to json_decode every image filenames and then use second foreach in your view file.
Controller
public function index()
{
$images = DB::select('select * from forms');
array_map(function($a) {
$a->filename = json_decode($a->filename);
return $a;
}, $images);
return view('index', compact($images)); // you can use compact function here
}
View
<ul class="list-group">
#foreach ($images as $image)
<li class="list-group-item">
#foreach ( $image->filename as $file)
{{ $file }}
#endforeach
</li>
#endforeach
</ul>
Edit: You can also try cast to array, then you dont need to worry about mapping it in your controller file.
I would like to have this effect:
<div class="w3-third">Images tags here</div>
<div class="w3-third">Images tags here</div>
<div class="w3-third">Images tags here</div>
PHP
$images = Image::where('gallery_id', $id)->get();
$list = [];
foreach ($images as $image) {
array_push($list, $image->file_path);
}
$list = array_chunk($list, 3);
return view('gallery.gallery')
->with([
'gallery' => $gallery,
'images' => $list
]);
And I want to push images from this list into those 3 columns.
In your view iterate over subarrays of images and subsubarrays of each images subarray:
#foreach ($images as $group)
<div class="w3-third">
#foreach ($group as $item)
{{ $item }}
#endforeach
</div>
#endforeach
I have a category table, it is organized by 'parent_id' and 'categoryid'. I need to organize it in a list, where I group the parent class with the daughter class.
I created this code.
In the controller I get the value of the categories.
public function index()
{
$cat1 = Category::where('erp_parentid', '=', 0)->get();
foreach($cat1 as $categoria1){
$cat2 = Category::where('erp_parentid', '=', $categoria1->erp_categoryid)->get();
return view('admin.categories')->with('cat1', $cat1)->with('cat2', $cat2);
}
}
$cat2 is the child category, I get its values through the categoryid of the parent category.
But when I pass the values to the view, all parent categories take the same value as the first.
I used that code to display the values in the view:
<div class="container">
<div class="row">
<ul class="list-group">
#foreach($cat1 as $value)
<a data-toggle="collapse" data-target="#catfilha{{$value->erp_categoryid}}"><li class="list-group-item">{{$value->erp_name}}</li></a>
<ul id="catfilha{{$value->erp_categoryid}}" class="collapse">
#foreach($cat2 as $value2)
<li>{{$value2->erp_name}}</li>
#endforeach
</ul>
#endforeach
</ul>
</div>
</div>
I searched for similar cases here on the site, but found no resemblance, any suggestions? Thank you in advance.
You should define the relations in the model and call them in the view. Try something like this:
In Category Model:
public function getParent()
{
return self::where('erp_parentid', '=', $this->erp_categoryid)->get();
}
In Controller:
public function index()
{
$cat1 = Category::where('erp_parentid', '=', 0)->get();
return view('admin.categories')->with('cat1', $cat1);
}
In the View:
<div class="container">
<div class="row">
<ul class="list-group">
#foreach($cat1 as $value)
<a data-toggle="collapse" data-target="#catfilha{{$value->erp_categoryid}}"><li class="list-group-item">{{$value->erp_name}}</li></a>
<ul id="catfilha{{$value->erp_categoryid}}" class="collapse">
#foreach($value->getParent() as $value2)
<li>{{$value2->erp_name}}</li>
#endforeach
</ul>
#endforeach
</ul>
</div>
</div>
In your code, the return statement is inside the loop, so cat2 will be always the categories from the first item of cat1.
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.