Laravel/PHP - loop through items in a view - php

i have the following html / blade template:
{{--show all projects--}}
#if(!empty($projects))
#foreach($projects as $project)
<div class="slide">
<ul class="small-block-grid-1 medium-block-grid-3 large-block-grid-4">
<li>
<div class="thumb">
<h3 class="thumbTitle">{{$project->title}}</h3>
{{--laat maar 1 image zien --}}
#if(!empty($project->project_images))
{{ HTML::image('useruploadedimages/'.$project->project_images[0]->url) }}
#endif
</div>
</li>
</div>
#endforeach
#endif
</div>
what i want is the following: 8 projects displayed within a div with the .slide class, multiple divs with the .slide class for ervery 8 projects: for example i have 25 projects: than i want to have 4 .slide divs 3 with 8 projects and 1 with a single project...
is this even possible?

You may use array_chunk like this:
#foreach(array_chunck($projects->toArray(), 8) as $projects)
<div class="slide">
<ul class="small-block-grid-1 medium-block-grid-3 large-block-grid-4">
#foreach($projects as $project)
<li>
<div class="thumb">
<h3 class="thumbTitle">{{$project['title']}}</h3>
#if(!empty($project['project_images']))
{{HTML::image('....)}}
#endif
</div>
</li>
#endforeach
</ul>
</div>
#endforeach
Notice the array notation [] instead of object notation ->.

You can try this peace of code
{{--show all projects--}}
#if(!empty($projects))
$i = 8;
#foreach($projects as $project)
#if($i%8 == 0) // checks for multiples if 8 and wraps your content with div and ul
<div class="slide">
<ul class="small-block-grid-1 medium-block-grid-3 large-block-grid-4">
#endif
<li>
<div class="thumb">
<h3 class="thumbTitle">{{$project->title}}</h3>
{{--laat maar 1 image zien --}}
#if(!empty($project->project_images))
{{ HTML::image('useruploadedimages/'.$project->project_images[0]->url) }}
#endif
</div>
</li>
#if($i++%8 == 0) // checks for multiples if 8 and increments i value
</ul> // ends the ul and div
</div>
#endif
#endforeach
#endif
</div>
hope it helps

You can close and open a new .slide div for every 8 projects. I supose the ul has to restart in every div as well?
#if(!empty($projects))
<div class="slide">
<ul class="small-block-grid-1 medium-block-grid-3 large-block-grid-4">
#for($i = 0; $i < count($projects); $i++)
<?php $project = $projects[$i]; ?>
<li>
<div class="thumb">
<h3 class="thumbTitle">{{$project->title}}</h3>
{{-- only show one image --}}
#if(!empty($project->project_images))
{ HTML::image('useruploadedimages/'.$project->project_images[0]->url) }}
#endif
</div>
</li>
{{-- If there were 8 projects, start a new .slide --}}
#if ($i % 8 == 7)
</ul></div><div class="slide"><ul class="small-block-grid-1 medium-block-grid-3 large-block-grid-4">
#endif
#endfor
</ul>
</div>
#endif

Related

Divide by 4 for gallery

sorry for asking here but I can't seem to get my head around what I need to do to complete this.
I have a gallery that is pulled from the database and i'd like it so every division of 4 it adds a new row.
Here is my website: https://creativehedgehog.co.uk/gallery
i.e
<div class="row">
col3
col3
col3
col3
</div>
Is this correct? Here is my code
<div class="gallery-container">
<div class="gallery cf">
#php
$i=0;
#endphp
#foreach($galleries as $gallery)
#if($i % 4 == 0)
{{$i}}
<div class="row">
#endif
<div class="col-md-3">
<a href="{{$gallery->link}}" data-lightbox="image-1" data-title="{{$gallery->title}}">
<img src="{{$gallery->link}}" alt="{{$gallery->title}}">
</a>
</div>
#if($i % 4 == 0)
{{$i}}
</div>
#endif
#php
$i++;
#endphp
#endforeach
</div>
</div>
Use array_chunk function. array_chunk
<div class="gallery-container">
<div class="gallery cf">
#foreach(array_chunk($galleries,4,true) as $chank)
<div class="row">
#foreach($chank as $gallery)
<div class="col-md-3">
<a href="{{$gallery->link}}" data-lightbox="image-1" data-title="{{$gallery->title}}">
<img src="{{$gallery->link}}" alt="{{$gallery->title}}">
</a>
</div>
#endforeach
</div>
#endforeach;
</div>
</div>
You are adding a row element every fourth iteration, not placing 4 iteration into each row.
Look below:
<div class="gallery-container">
<div class="gallery cf">
#php
$i=0;
#endphp
#foreach($galleries as $gallery)
#if($i % 4 == 0)
#if($i != 0)
</div>
#endif
{{$i}}
<div class="row">
#endif
<div class="col-md-3">
<a href="{{$gallery->link}}" data-lightbox="image-1" data-title="{{$gallery->title}}">
<img src="{{$gallery->link}}" alt="{{$gallery->title}}">
</a>
</div>
#php
$i++;
#endphp
#endforeach
</div><!-- last row end -->
</div>
</div>
Try this and let the bootstrap flexbox do their job
<div class="gallery-container">
<div class="gallery cf">
<div class="container-fluid">
<div class="row">
#foreach($galleries as $gallery)
<div class="col-md-3">
<a href="{{$gallery->link}}" data-lightbox="image-1" data-title="{{$gallery->title}}">
<img src="{{$gallery->link}}" alt="{{$gallery->title}}">
</a>
</div>
#endforeach
</div>
</div>
</div>
</div>

Insert a div in random intervals Laravel collection

I have a laravel application which is an ecommerce store.
On my shop category which contains rows of products I'd like to randomly in between the products insert a div.
This div should be random throughout the page.
For example, I have the following (I've pruned the code to keep it clean):
#section('content')
<div class="products">
#foreach($page->products->chunk(6) as $chunk)
<div class="group">
#foreach($chunk as $product)
<div class="category_product">
<div class="category_product_info">
<h2>
<a href="{{ $product->slug->slug }}">
{{ $product->title }}
</a>
</h2>
</div>
</div>
#endforeach
</div>
#endforeach
</div>
#endsection
In the chunk of 6 is it possible to inject say a seventh element which would appear randomly on the row?
I don't see what information you are trying to add into your 7th div, but here is how I see it: check for each loop if a random between 1 and 100 it higher than your resired random rate ( in my case 50% ) , if so add the div.
#section('content')
<div class="products">
#foreach($page->products->chunk(6) as $chunk)
<div class="group">
#foreach($chunk as $product)
<div class="category_product">
<div class="category_product_info">
<h2>
<a href="{{ $product->slug->slug }}">
{{ $product->title }}
</a>
</h2>
</div>
</div>
#endforeach
</div>
#if (rand(1, 100) > 50)
<div class=randomdiv></div>
#endif
#endforeach
</div>
#endsection
You would apply the same process with your chuck, instead of 6 , add a one line if ( i don't know how they are called ). something like #foreach($page->products->chunk(rand(1, 100) > 50 ? 6 : 7) as $chunk)

PHP or Blade - Organized Columns depending on the number of data

I'm blocked for organize my sub-menu of my menu. As you can see,
it is decomposed into several blocks (i've use paint for delimited the block in black).
currently my code is:
<li class="dropdown yamm-fw">
{{ $category->nom }} <b class="caret"></b>
<ul class="dropdown-menu">
<li>
<div class="yamm-content">
<div class="row">
<div class="col-sm-3">
<h5>test</h5>
<ul>
<?php $count=0 ?>
#foreach ($types->whereIn('id', $category->produit->unique('type_id')->pluck('type_id')) as $type)
#if ($count <= 6)
<li>{{$type->nom}}</li>
<?php $count++ ?>
#endif
#endforeach
</ul>
</div>
<div class="col-sm-3">
<h5>teste 2</h5>
<ul>
<li>Trainers</li>
<li>Sandals</li>
<li>Hiking shoes</li>
<li>Casual</li>
</ul>
</div>
I do not know how to do that after 7 items in the actual div col-sm-3, create another div to continue the foreach and so on.
You can use the $loop variable:
#if ($loop->iteration === 7)
// Create another div here
#endif

Make a function containing blade syntax

I have this blade in my view. Right now, I have 6 blocks of them in my view because I'm not sure how to refactor it.
<div class="row filemanager">
<div class="col-sm-12">
#foreach ($devices as $device)
#if( $device->vlan_id == 100 AND $device->device_activity == 'ACTIVE' )
<div class="col-xs-6 col-sm-4 col-md-2 text-center">
<div class="thmb">
<div class="btn-group fm-group" style="display: none;">
<button type="button" class="btn btn-default dropdown-toggle fm-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu fm-menu" role="menu">
<li id="device-menu">
<a class="changeDeviceNameBtn" href="#"><i class="fa fa-pencil"></i> Change Device Name </a>
</li>
</ul>
</div>
<div class="thmb-prev">
<a href="/{{$cpe_mac}}/device/{{$device->device_mac}}">
#if(isset($device->device_name))
{{-- Show base on device name --}}
<img src="/images/photos/devices/{{img($device->device_name)}}.jpg" class="draggable img-responsive" alt="">
#else
{{-- No Device Name Set --}}
#if($device->hostname != '')
{{-- Show base on hostname --}}
<img src="/images/photos/devices/{{img($device->hostname)}}.jpg" class="draggable img-responsive" alt="">
#else
{{-- Show default --}}
<img src="/images/photos/devices/no-img.jpg" class="draggable img-responsive" alt="">
#endif
#endif
</a>
</div>
<h5 class="fm-title device_name">
<a href="/{{$cpe_mac}}/device/{{$device->device_mac}}">
#if($device->hostname == '')
No Devicename
#else
{{ $device->hostname}}
#endif
</a>
</h5>
<h5 class="text-muted device_ip">{{$device->ip_address}}</h5>
<h5 class="text-muted device_mac">{{$device->device_mac}}</h5>
<?php
$status = ucfirst(strtolower($device->device_activity));
if ($status == 'Active'){
$color = '#1CAF9A';
}else{
$color = '#D9534F';
}
?>
<h5>{{ $status }}
<i class="fa fa-circle" style="color:{{$color}}; margin-left: 7px;"></i>
</h5>
</div>
</div>
#endif
#endforeach
</div>
</div>
I want to make a function containing that blade, and only replace my
$device->vlan_id, and my $device->device_activity.
Example,
public static deviceRow(100,ACTIVE){
... my blade ...
}
Now, I just that function 6 times, rather than duplicate that block of code 6 times.
Is it even possible ?
Any hints / suggestions on this will be much appreciated !
You can make a partial with your blade and send a variable as a parameter:
In your parent view do something like this:
#foreach($somelist as $item)
#include('view.partial', ['name' => $item->name])
#endforeach
And in a file called partial.blade.php, do something like this:
{{ $device->$name }}
It's the main idea. Tell me if it helps...
You could create a new view and send some parameters with it while including:
#include('my.view', ['device' => $myDevice, 'activity' => 'ACTIVE'])
The keys of the array will be available as variables in your view.
The variable $myDevice would be available as $device in the view my.view

Laravel 4 Trying to get property of non-object error

I was working on my website and I logged out to test it and it gave me this error:
Trying to get property of non-object (View: C:\xampp\htdocs\mom\a\app\views\home.blade.php)
Here is my home.blade.php:
#extends('layout.main')
#section('title') Home #stop
#section('content')
<!-- User signed in -->
#if(Auth::check())
<!-- User is not admin -->
<p>Welcome back, {{ Auth::user()->username }}.</p>
#else
<!-- Nothing -->
#endif
#if($posts->count())
#foreach($posts as $post)
<article class="col-md-8 col-md-push-2 well well-white home-posts">
<h2 class="title">{{ $post->title }}</h2>
<hr class="post-break">
<h4 class="home-content">{{ Markdown::parse(Str::limit($post->body, 300)) }}</h4>
<div class="post-footer">
<ul class="footer-group">
<ul>
<li>Published {{ $post->created_at->diffForHumans() }}</li>
<li> Read more →</li>
</ul>
#if(Auth::user()->group ==1)
<ul class="footer-right">
<li class="red"><i class="fa fa-trash"></i> Delete</li>
<li><i class="fa fa-gear"></i> Edit</li>
</ul>
#else
<!-- Nothing -->
#endif
</ul>
</div>
</article>
#endforeach
#endif
<div class="col-md-8 col-md-push-2 ">{{ $posts->appends(array())->links() }}</div>
#stop
If someone could help me find out what property is of the non object is being called and help me fix it. That would be great. Much appreciated.
You should put this line
<div class="col-md-8 col-md-push-2 ">{{ $posts->appends(array())->links() }}</div>
in your if statement
EDIT:
Try to change line:
#if(Auth::user()->group ==1)
to
#if(Auth::check() && Auth::user()->group ==1)

Categories