I am new to Laravel. I am trying a loop through a multiple div and struck in between. I want to wrap a div with multiple columns in it.
#foreach($products as $product)
<div>
<div class="row">
#foreach($products as $product)
<div class="col-md-4">
{{image}}
</div>
#endforeach
</div>
</div>
#endforeach
It's not printing the expected output. my expected output should be like.
<div>
<div class="row">
<div class="col-md-4">image1</div>
<div class="col-md-4">image2</div>
<div class="col-md-4">image3</div>
</div>
</div>
<div>
<div class="row">
<div class="col-md-4">image4</div>
<div class="col-md-4">image5</div>
<div class="col-md-4">image6</div>
</div>
</div>
How can I print div properly with the above formate?
Try using Collection Chunks:
#foreach($products->chunk(3) as $chunk)
<div class="row">
#foreach($chunk as $product)
<div class="col-md-4">
YOUR IMAGE HERE
</div>
#endforeach
</div>
#endforeach
Try this?
<div class="row">
#for($i = 1; $i <= len($products); $i++)
<div class="col-md-4">
Title: {{$products[$i-1]->title }}
</div>
#if($i%3===0)
</div> <div class="row">
#endif
#endfor
</div>
#for($i = 0; $i < count($products); $i++)
<div>
<div class="row">
#foreach($products as $product)
<div class="col-md-4">
{{image}}
</div>
#endforeach
</div>
</div>
#endfor
Related
How to show label in a loop.For example label 1, label 2 and so on continuous till the end of the loop.
Here is my code i want to show Experience 1 and Experience 2 and so on through out the loop
till loop ends.
#foreach($employee->experiences as $experince)
<div class="card-header">
<label for="">Experience</label>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<div class="col-md-6"><b>Company:</b></div>
<div class="col-md-6">{{ ucfirst($experince->company) }}</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group row">
<div class="col-md-6"><b>Degree :</b></div>
<div class="col-md-6">{{ ucfirst($employee->degree) }}</div>
</div>
</div>
</div>
</div>
#endforeach
In blade template there is a $loop variable:
$loop->index
will print the current index, starting from 0
$loop->iteration
will instead start from 1
The variable is available inside a #foreach:
#foreach($employee->experiences as $experince)
{{ $loop->iteration }}
#endforeach
Try this $loop->iteration ref link https://laravel.com/docs/8.x/blade#the-loop-variable
#foreach($employee->experiences $experince)
<div class="card-header">
<label for="">Experience {{ $loop->iteration }}</label>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<div class="col-md-6"><b>Company:</b></div>
<div class="col-md-6">{{ ucfirst($experince->company) }}</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group row">
<div class="col-md-6"><b>Degree :</b></div>
<div class="col-md-6">{{ ucfirst($employee->degree) }}</div>
</div>
</div>
</div>
</div>
#endforeach
Create it variable as iteration and increment it each iterations
#php
$it = 1;
#endphp
#foreach($employee->experiences as $experince)
<div class="card-header">
<label for="">Experience</label>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<div class="col-md-6"><b>Label:{{ $it }}</b></div>
<div class="col-md-6"><b>Company:</b></div>
<div class="col-md-6">{{ ucfirst($experince->company) }}</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group row">
<div class="col-md-6"><b>Degree :</b></div>
<div class="col-md-6">{{ ucfirst($employee->degree) }}</div>
</div>
</div>
</div>
</div>
#php
$it += 1;
#endphp
#endforeach
I get a list of number and series. But when i dump data in to a view it appears like this:
[{"id":1,"number":"e379079p272730","series":"88000000001","type":"import","group_series":null}]
I want on the view it will display the number value in the number column and the series value in the series column. But I don't know how to do that?
View admin.student.listcard
<div class="card-block" >
<div class="row">
<div class="col-md-3 ">
<div class="card-title">
<strong>Number</strong>
</div>
</div>
<div class="col-md-2">
<div class="card-title">
<strong>Series</strong>
</div>
</div>
</div>
#foreach($orders as $order)
<div class="row bottom">
<div class="col-md-3">
<div class="form-group">
{{ $order->card }}
</div>
</div>
<div class="col-md-2">
<div class="form-group">
</div>
</div>
</div>
#endforeach
</div>
function
public function listCard($studentId)
{
$orders = Order::where('member_id',$studentId)->get();
foreach($orders as $order){
$order->card = Card::where('id',$order->card_id)->get();
}
return view('admin.student.listcard',compact('orders'));
}
You have to use eloquent relationships !
In your Order model :
public function card()
{
return $this->belongsTo(Card::class);
}
In your controller :
$orders = Order::with('card')->where('member_id',$studentId)->get();
In your view :
#foreach($orders as $order)
<div class="row bottom">
<div class="col-md-3">
<div class="form-group">
{{ $order->card->number }}
</div>
</div>
<div class="col-md-2">
<div class="form-group">
{{ $order->card->series }}
</div>
</div>
</div>
#endforeach
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>
I have a foreach loop and inside that contains html with bootstrap columns.
#foreach($address as $add)
<div class="col-md-6">
Some data
</div>
#endforeach
However, bootstrap requires the row div before creating columns, placing that straight in to the foreach loop would create a row div for each col-md-6. I want to know how I can throw in the row div, skip the next loop throwing in only the closing div tag. And then repeat that process.
Example output where the loops 4 times:
<div class="row">
<div class="col-md-6">
Some data
</div>
<div class="col-md-6">
Some data
</div>
</div>
<div class="row">
<div class="col-md-6">
Some data
</div>
<div class="col-md-6">
Some data
</div>
</div>
As an alternative to Alexey Mezenin's answer you could use array_chunk instead. http://php.net/manual/en/function.array-chunk.php
#foreach(array_chunk($address, 2) as $chunk)
<div class="row">
#foreach($chunk as $add)
<div class="col-md-6">
Some data
</div>
#endforeach
</div>
#endforeach
I personally find the the above a little more readable.
Alternatively, if $address is a collection you could do $address->chunk(2) instead of array_chunk($address, 2).
If you want to change the amount of columns you have you would simply need to change the 2 to be however many columns you want.
You can use Laravel chunk in the blade template.
#foreach($products->chunk(3) as $items)
<div class="row">
#foreach($items as $item)
<div class="col-md-4 portfolio-item">
<a href="#">
<img class="img-responsive" src="{{ 'uploads/'.$item->product_image_url }}" alt="">
</a>
<h3>
{{ $item->product_name }}
</h3>
<p>{{ str_limit($item->product_description, 121) }}</p>
</div>
#endforeach
</div>
#endforeach
Copied from the blogpost.
Use the $loop variable:
<div class="row">
#foreach($address as $add)
<div class="col-md-6">
Some data
</div>
#if ($loop->iteration % 2 == 0)
</div>
<div class="row">
#endif
#endforeach
</div>
I need to create a row for each 3 attachments. Something like
#foreach($email->attachment as $attach)
<div class="row">
<div class="col-lg-4"></div>
<div class="col-lg-4"></div>
<div class="col-lg-4"></div>
</div>
#endforeach
<div class="row">
<div class="col-lg-4">1st attachment</div>
<div class="col-lg-4">2nd attachment</div>
<div class="col-lg-4">3rd attachment</div>
</div>
<div class="row">
<div class="col-lg-4">4th attachment</div>
<div class="col-lg-4">5th attachment</div>
<div class="col-lg-4">6th attachment</div>
</div>
and so on! I had searched for the question but just found This link but It's actually different from my perspective.
Thanks
Use array_chunk - it is designed for this each purpose - so you can cut an array into sub-sizes and loop through each group
#foreach (array_chunk($email->attachment->toArray(), 3, true) as $array)
<div class="row">
#foreach($array as $attachment)
<div class="col-lg-4">{{ $attachment['id'] }}</div>
#endforeach
</div>
#endforeach
And using #Lukasgeiter's comment - you could also do it this way if it is a Laravel Collection
#foreach ($email->attachment->chunk(3) as $array)
<div class="row">
#foreach($array as $attachment)
<div class="col-lg-4">{{ $attachment->id }}</div>
#endforeach
</div>
#endforeach
Try this, it's faster than generate 2 arrays:
<div class="row">
#foreach( $email->attachment as $index => $attach)
<div class="col-lg-4">{{$attach}}</div>
#if( $index+1 === 0)
</div><div class="row">
#endif
#endforeach
</div>