I don't really know the title of this one and tried searching but I don't have any idea on what keywords should I put it on so I create a new one but if there is already a similar post to this. Can you link it? My question is how do I achieve this goal in the image below.
#php $i = 0; #endphp
#foreach ($posts as $key => $post)
#if ($key % 2 === 0)
<div class="row">
<div class="col-md-4"><img src="{{ $post->image }}" /></div>
<div class="col-md-8">
- {{ $i+=1 }}
- {{ $i+=2 }}
- {{ $i+=3 }}
</div>
</div>
#else
#endif
#endforeach
The alternate class from bootstrap is working but the numbers that would increment on each value is I don't have any idea on how to get it done.
I'm not familiar with Larval syntax, but for the logic I think you should have something more like this:
#php $i = 0; #endphp
#foreach ($posts as $key => $post)
#if ($key % 2 === 0)
<div class="row">
<div class="col-md-4"><img src="{{ $post->image }}" /></div>
<div class="col-md-8">
- {{ ++$i }}
- {{ ++$i }}
- {{ ++$i }}
</div>
</div>
#else
<div class="row">
<div class="col-md-8">
- {{ ++$i }}
- {{ ++$i }}
- {{ ++$i }}
</div>
<div class="col-md-4"><img src="{{ $post->image }}" /></div>
</div>
#endif
#endforeach
Things to note that I changed:
1. Increment $i by one for EACH number, otherwise you'll be skipping numbers
2. When $key is NOT event, print the image on the other side.
Again, I'm not familiar with larval syntax, and I also don't know what the key/values of your $posts array are (if your keys aren't alternating even/odd you won't have the images back-and-forth between left-and-right).
Hopefully this gives you some direction though.
Related
I have 5 items ( 3 items on first row & 2 items on 2nd row ) that loads something like this
Here's my sample blade code
<div class="row">
#if( !empty( $data ) )
#foreach( $data as $field )
<div class="col-lg-4 services-overview-section__item">
<div class="services-overview-section__item--image">
<a href="{!! url('services-detail/'.$field->slug) !!}">
<img src="{{url($field->banner_image) }}">
<div class="services-overview-section__item--overlay">
<h3>{!! $field->name !!}</h3>
</div>
</a>
</div>
<div class="services-overview-section__item--content">
{!! $field->overview_description !!}
</div>
</div>
#endforeach
#endif
</div>
But what I'm trying to do is something like this
By doing this I need to add a class named as 'mid' on this location
<div class="col-lg-4 services-overview-section__item mid">
Tried to use something like this
<div class="col-lg-4 services-overview-section__item {{ $loop->iteration % 2 == 0 ? 'mid' : '' }}">
But it doesn't give me the right output like what I expected.
I just want to add class 'mid' for every 2nd item of the rows
A simple solution :
<div class="row">
#if( !empty( $data ) )
#php $itemThatNeedClass = 1; #endphp
#foreach( $data as $field )
#php
$customClass = "";
if ($loop->index === $itemThatNeedClass) {
$customClass = "mid";
$itemThatNeedClass += 3;
}
#endphp
<div class="col-lg-4 services-overview-section__item {{ $customClass }}">
<div class="services-overview-section__item--image">
<a href="{!! url('services-detail/'.$field->slug) !!}">
<img src="{{url($field->banner_image) }}">
<div class="services-overview-section__item--overlay">
<h3>{!! $field->name !!}</h3>
</div>
</a>
</div>
<div class="services-overview-section__item--content">
{!! $field->overview_description !!}
</div>
</div>
#endforeach
#endif
</div>
So I'm building my first blog, and I'd like to dynamically show to posts on my landing page. I want the layout to become like this (might be a little weird):
post-image - post-preview
post-preview - post-image
post-image - post-preview
This is what I have right now to show to blog posts:
#foreach($posts as $post)
<div class="col-sm-4">
<a href="/post/{{ $post->slug }}">
<img src="{{ Voyager::image( $post->image ) }}" class="img-responsive" style="width:100%">
<span>{{ $post->title }}</span>
</a>
</div>
#endforeach
web.php (route):
$posts = App\Post::take(3)->orderBy('created_at')->get();
return view('welcome', compact('posts'));
Now as I'd like to make the layout dynamically. I think I can do this by checking if the ID even or odd. However I have no idea on how to do this even when I've searched 50% of the web already :) .
If there is a better way on doing this just let me know! Greatly appreciated guys!
Yes you can do it by checking if current post or loop iteration is odd or even. (https://laravel.com/docs/5.5/blade#the-loop-variable) For Odd loop iteration, you can write image code first and in even loop iteration you can write preview code first to achieve your desired layout. You can check the following code:
#foreach($posts as $post)
<div class="row">
#if( $loop->iteration % 2 == 0 )
<div class="col-md-4">
<a href="/post/{{ $post->slug }}">
<img src="{{ Voyager::image( $post->image ) }}" class="img-responsive" style="width:100%">
</a>
</div>
<div class="col-md-8">
<span>{{ $post->title }}</span>
</div>
#else
<div class="col-md-8">
<span>{{ $post->title }}</span>
</div>
<div class="col-md-4">
<a href="/post/{{ $post->slug }}">
<img src="{{ Voyager::image( $post->image ) }}" class="img-responsive" style="width:100%">
</a>
</div>
#endif
</div>
#endforeach
Ok I guess I got your question. so there is no direct solution but you can measure inside your for loop by adding extra variable such as:
<?php $inc = 0;?>
#foreach($posts as $post)
<div class="col-sm-4">
<a href="/post/{{ $post->slug }}">
#if($inc%2 == 0)
<img src="{{ Voyager::image( $post->image ) }}" class="img-responsive" style="width:100%">
<span>{{ $post->title }}</span>
#else
<span>{{ $post->title }}</span>
<img src="{{ Voyager::image( $post->image ) }}" class="img-responsive" style="width:100%">
#endif
<?php $inc++;?>
</a>
</div>
#endforeach
Or you can use for loop instead of foreach of loop and inside for loop you can check variable i.e. i is odd or even.
I'm trying to set a condition check whether the chunk is the first batch of chunk or not.
Anyone know how to check what the chunk id or something similar for example:
#foreach($item->chunk(3) as $item_chunk
#if(chunk is first chunk)
<div class="carousel-item active">
#else
<div class="carousel-item">
#endif
#foreach($item_chunk as $item)
{{-- Do something --}}
#endforeach
#endforeach
Appreciate it if someone knows of a way to check whether the chunked list is first or not.
You can check loop using $loop variable.
#foreach($item->chunk(3) as $item_chunk)
#if($loop->first)
<div class="carousel-item active">
#else
<div class="carousel-item">
#endif
#foreach($item_chunk as $item)
{{-- Do something --}}
#endforeach
#endforeach
For sliders
<div class="carousel-inner">
#foreach($item_hr as $count => $valuetopproducts)
<div class="item {!! $count == 0 ? 'active' : '' !!}">
<img src="{{Storage::url('wp/thumbnail')}}/{{$valuetopproducts->imagename}}" class="img-responsive" alt="{!! \Illuminate\Support\Str::words($valuetopproducts->assetname, 10,'...') !!}">
</div>
#endforeach
</div>
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)
I am trying to fill my webpage with content based on content stored in a database. However, I would like to skip the first item; I want to start looping from the second item.
How can I achieve this?
#foreach($aboutcontent as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach
As of Laravel 5.4, whenever you use foreach or for within blade files you will now have access to a $loop variable. The $loop variable provides many useful properties and methods, one of them being useful here, for skipping the first iteration. See the example below, which is a much cleaner way of achieving the same result as the other older answers here:
#foreach ($rows as $row)
#if ($loop->first) #continue #endif
{{ $row->name }}<br/>
#endforeach
Try This :
#foreach($aboutcontent as $key => $about)
#if($key > 0){
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endif;
#endforeach
Assuming that the $aboutcontents is numeric array just use the good old fashioned for loop instead of your new fangled foreach
// Notice you start at 1 and your first
// elem is 0 so... ta da... skipped
#for ($i = 1; $i < count($aboutcontents); $i++){
$about = $aboutcontents[$i]; //This is the object
//now use $about as you would
}
Note: I have not used Larvel or blades but based on the docs this should be doable
There is two way to do this:
1- if your $key is numeric you can use:
#foreach($aboutcontent as $key => $about)
#if($key == 0)
#continue
#endif
code
#endforeach
2- if a $key is not numeric
use #loop->first as a condition
You need some kind of a counter if you want to do that in blade:
<?php $count = 0;?>
#foreach
#if($count>1)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endif
$count++
#endforeach
EDIT:
I like the answer provided by Mark Baker in the comment better
#foreach(array_slice($aboutcontent, 1) as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach
Alternatively, you can just remove the first element from the array before iterating:
#php
array_shift($aboutcontent);
#endphp
#foreach($aboutcontent as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach
The advantage is that you do not need any conditions to verify you are not the in the first iteration. The disadvantage is that you might need the first element within the same view, but that we don't know from your example.
Note It might make more sense to remove the first element from the array before you pass on the data to the view.
For reference, see:
http://php.net/manual/en/function.array-shift.php
https://laravel.com/docs/5.4/blade#php
Try this
#foreach($aboutcontent->slice(1) as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach