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>
Related
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
How to divide a file into multiple files with laravel or php?
For example:
There is a file demo.blade.php:
<div class="card-block">
<div class="row">
<div class="col-1">fruits</div>
<div class="col-11">
apple,pear,peach
</div>
</div>
</div>
<div class="card-block">
<div class="row">
<div class="col-1">aminals</div>
<div class="col-11">
cat,dog,calf
</div>
</div>
</div>
<div class="card-block">
<div class="row">
<div class="col-1">vehicles</div>
<div class="col-11">
car,bus,motorbike
</div>
</div>
</div>
I want to write some code to divide the demo.blade.php into three files:
fruits.blade.php
aminals.blade.php
vehicles.blade.php
The content is each <div class="card-block"></div>.
How to do it?
Any help would be appreciated.
You can use other blade in your blade file like this :
#include('fruits.blade.php')
#include('aminals.blade.php')
#include('vehicles.blade.php')
But this is not a good idea. Instead you can make a template like this :
<div class="card-block">
<div class="row">
<div class="col-1">{{ $title }}</div>
<div class="col-11">
{{ $content }}
</div>
</div>
</div>
And use it everywhere :
#include('template', ['title' => 'foo', 'content' => 'bar'])
Anyway I don't know your purpose of doing this. And you can explain more to give you more examples.
You can also use Blade Components
//This is the template cardblock.blade.php
<div class="card-block">
<div class="row">
<div class="col-1">{{ $title }}</div>
<div class="col-11">
{{ $slot }}
</div>
</div>
</div>
And you can use it like this
#component('cardblock')
#slot('title')
fruits
#endslot
apple,pear,peach
#endcomponent
#component('cardblock')
#slot('title')
aminals
#endslot
cat,dog,calf
#endcomponent
#component('cardblock')
#slot('title')
vehicles
#endslot
car,bus,motorbike
#endcomponent
I am making a forum and when the user clicks on a theme, a page shows up with every topic that belongs to that theme. The problem is, how do I do this?
I made a for each loop which shows ALL the topics from the database instead of the topics that belong to that theme I clicked on. How can I do this?
Web.php
Route::get('/', 'ThemesController#index')->name('home');
Route::get('/theme/{id}', 'ThemesController#show')->name('showtheme');
ThemesController.php
I only show the show method because I believe that's the only one necessary here
public function show($id)
{
$topics = Topic::all($);
return view('themes/topics')->with('topics', $topics);
}
topics.blade.php
#extends('layouts.default')
#section('content')
<div class="container main-content">
<div class="row first-row">
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content"><span class="card-title"> - Topics</span>
<div class="collection">
#foreach($topics as $topic)
<a href="" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
<div class="row">
<div class="col s6">
<div class="row last-row">
<div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
<p>{!! str_limit($topic->topic_text, $limit = 100, $end = '...') !!}</p>
</div>
</div>
<div class="row last-row">
<div class="col s12 post-timestamp">Gepost door: {{ $topic->user->username }} op: {{ $topic->created_at }}</div>
</div>
</div>
<div class="col s2">
<h6 class="title center-align">Replies</h6>
<p class="center replies">{{ $topic->replies->count() }}</p>
</div>
<div class="col s2">
<h6 class="title center-align">Status</h6>
<div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
</div>
<div class="col s2">
<h6 class="title center-align">Laatste reply</h6>
<p class="center-align">Naam</p>
<p class="center-align">Tijd</p>
</div>
</div>
</a>
#endforeach
</div>
</div>
</div>
</div>
<div class="col s12">
<div class="card">
<div class="card-content clearfix">Nieuw topic</div>
</div>
</div>
</div>
</div>
#endsection
I believe I'm doing something obviously wrong but I can't see what.
Help will be appreciated. Thanks in advance
If I miss some code, Please inform me.
How are you defining the relationship between a theme and it's topics? You'll have to have something like a one to many or many to many established in your database for you to be able to query correctly. Do you have anything like that yet?
****edit****
public function show($id)
{
$topics = Theme::find($id)->topics;
return view('themes/topics')->with('topics', $topics);
}
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 have seen as "there are no posts" if they are available as comment even the URL is working property.
I have discover on laravel.com there is not lack of information or example inside the #if and #else . http://laravel.com/docs/4.2/templates#other-blade-control-structures.
here is my code is
#if ($post->comments)
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#endif
#foreach ($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#endforeach
however I have tried change as #if ($post->comments as $comment) is said "syntax error, unexpected 'as' (T_AS)" do you have any idea to solve it?
or you could use the blade's #forelse loop this way
#forelse($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#empty
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#endforelse
Just a guess, but I think this is what you wanted to do
#if ($post->comments)
#foreach ($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#endforeach
#else
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#endif
You are trying to put a foreach in the condition of an if statement
Put the foreach in the entire #if, and check for NOT comments :
#if (!$post->comments)
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#else
#foreach ($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#endforeach
#endif
This should be the control flow you are looking for.