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
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>
I have created 2 collections(arrays) which each contain an array of 5 items/events.
$events = App\get_event_data( $args )
$collection = collect($events['events']);
$event_chunks = $collection->chunk(5);
$event_chunks->toArray();
#foreach ($event_chunks as $chunk)
Output of the above:
object(Illuminate\Support\Collection)[27632]
protected 'items' =>
array (size=2)
0 =>
object(Illuminate\Support\Collection)[27630]
protected 'items' =>
array (size=5)
...
1 =>
object(Illuminate\Support\Collection)[27631]
protected 'items' =>
array (size=5)
...
In my next loop, it simply goes through every item of the array 1 by 1.
I need to split the 5 items into a further 2 groups:
group of 2
group of 3
so I can wrap a div around each group.
Current loop:
#foreach ($chunk as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
What I need:
<div class="item-group">
[item1, item2]
</div>
<div class="item-group">
[item3, item4, item5]
</div>
Full code:
{{-- Get all events --}}
#if( $events = App\get_event_data( $args ) )
#php
$collection = collect($events['events']);
$event_chunks = $collection->chunk(5);
$event_chunks->toArray();
#endphp
#foreach ($event_chunks as $chunk)
<div class="{{ $block }}__flex-grid">
#foreach ($chunk as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
</div>
#endforeach
#endif
Assuming that you will allways have 5 items, a solution could be:
<div class="item-group">
#foreach (array_slice($chunk->toArray(),0,2) as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
</div>
<div class="item-group">
#foreach (array_slice($chunk->toArray(),2) as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
</div>
Or if want to avoid code duplication:
#php $chunk = [array_slice($chunk->toArray(),0,2), array_slice($chunk,2)];
#foreach ($chunk as $group)
<div class="item-group">
#foreach ($group as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
</div>
#endforeach
If you don't know or not sure that you have 5, you may need to change the logic of the chunks/slice.
It's maybe too late to answer, but I've made a function to split arrays into as groups as you like.
function SplitInto($array, $groups = 2)
{
$members = count($array) / $groups;
if (count($array) % $groups) $members = (int)(count($array) / $groups) + 1;
return array_chunk($array, $members);
}
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 get errors in laravel how do I echo the values correctly. I have this multidimensional array. This is the controller:
$data = array('names'=>$names, 'fruits'=>$fruits);
return view('content', [
'data' => $data
]);
and here is where I echo the values:
#section('content')
<div class="row">
<div class="col-md-12">
#foreach ($data as $row)
{{$row['fruits']}}
#endforeach
</div>
</div>
#endsection
You can send an array with key and values to your views. The key will be the variable name in your view. This is all very basic Laravel stuff and you can read more about it in the docs.
The controller:
$data = array('names'=>$names, 'fruits'=>$fruits);
return view('content', $data);
You view:
#section('content')
<div class="row">
<div class="col-md-12">
#foreach ($fruits as $fruit)
<!-- Do stuff with $fruit -->
#endforeach
</div>
</div>
#endsection
If you want to loop over them you could do that with your original controller, but this is how it would look in your view:
#section('content')
<div class="row">
<div class="col-md-12">
#foreach ($data as $item)
{{ $item }}
#endforeach
</div>
</div>
#endsection
This will only work if $fruits and $names are convertible to strings. Otherwise you would get an error.
$data is also an array. Access it's values with the keys $data["fruits"] or $data["names"]. If you want to loop through the $fruits-array just use a foreach-loop in your template:
foreach ($data["fruits"] as $key => $value) {
// make something with the fruit-entries.
}
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.