<h2 class="section-subheading text-muted text-center">الكورس الاول</h2>
<div class="row text-center">
#foreach ($data as $item)
<div class="col-md-4 px-3">
<div class="card shadow p-4">
<h5 class="text-center">{{$item->name}}</h5>
<hr>
show lectures
</div>
</div>
#endforeach
<h2 class="section-subheading text-muted text-center">الكورس الثاني</h2>
#foreach ($data2 as $item)
<div class="col-md-4 px-3">
<div class="card shadow p-4">
<h5 class="text-center">{{$item->name}}</h5>
<hr>
show lectures
</div>
</div>
#endforeach
</div>
If I'm understanding your question correctly.
You can nest #foreach statements like so
#foreach ($list1 as $item1)
...
#foreach ($list2 as $item2)
...
#endforeach
...
#endforeach
You would obviously want to use better names for the variables than in my sample. You do not need to have additional statements between the loops. The "..." is there to show you can.
#foreach ($data as $item)
{{ $item->name }}
#foreach ($data2 as $item2)
{{ $item2->name }}
#endforeach
#endforeach
Related
I'm trying to filter some products by categories using the Dropdown. The filter worked very well, but the category in the Dropdown only showed the categories result, e.g there's a Coffee, and Tea category, if I select Tea, the dropdown only showed the Tea category, unlike before, showing every category available.
Controller :
if(request()->category_filter){
$resultCategory = request()->category_filter;
$products = Product::where('category_id', $resultCategory)->with(['category', 'image'])->get();
}else{
$products = Product::with(['category', 'image'])->get();
}
return view('home.products', compact('products'));
View :
#extends('home.layouts.app')
#section('page-content')
<section class="section-padding" id="products">
<div class="container">
<div class="row">
<div class="col-12">
<h2 class="mb-5 text-center">Products</h2>
</div>
<div class="col-2 filter mb-3 text-center">
<div class="row">
<p>Category</p>
<div class="col">
<form action="{{ route('homepage.products') }}" method="get">
<select name="category_filter" id="category_filter" class="form-select mb-2">
#foreach ($products as $p)
<option value="{{ $p->category->id }}">{{ $p->category->name }}</option>
#endforeach
</select>
<button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-search"></i> Search</button>
<i class="fas fa-arrow-rotate-left"></i> Reset
</form>
</div>
</div>
</div>
<div class="row">
#foreach ($products as $p)
<div class="col-3">
<div class="portfolio-thumb mb-5">
#foreach ($p->image as $image)
<a href="{{ asset('storage/'.$image->path) }}" class="image-popup">
<img src="{{ asset('storage/'.$image->path) }}" class="img-fluid portfolio-image" alt="">
</a>
#break
#endforeach
<div class="portfolio-info">
<h5 class="text-black">{{ $p->name }}</h5>
<h6 style="color:{{ $p->category->indicator }};">{{ $p->category->name }}</h6>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
</section>
#endsection
You should query the categories separately from the products, so when you filter the products, the category list will not be affected.
if (request()->category_filter) {
$resultCategory = request()->category_filter;
$products = Product::where('category_id', $resultCategory)->with(['image'])->get();
} else {
$products = Product::with(['image'])->get();
}
$categories = Category::whereHas('products')->get();
return view('home.products', compact('products', 'categories'));
Then in your blade, just loop the $categories variable
<select name="category_filter" id="category_filter" class="form-select mb-2">
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
I'm using the materialisecss CSS framework within my Laravel Views, and I'm trying to set my layout such that:
if there is only one post available, it is displayed 6 columns wide,
offset by 3
if there are only 2 posts, they are both displayed 6 columns wide side by side
if there are more than 2 posts they are displayed 4 columns wide.
I have the code below as part of my view, but it doesn't like it, and returns the following:
Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) (View: /home/testuser/sites/lsapp/resources/views/posts/index.blade.php)
#if(count($posts) > 0)
<div class="row">
#if (count($posts) === 1)
#foreach($posts as $post)
<div class="col s12 m6 offset-m3">
#elseif (count($posts) === 2)
#foreach($posts as $post)
<div class="col s12 m6">
#else
#foreach($posts as $post)
<div class="col s12 m4">
#endif
<a href="posts/{{$post->id}}">
<div class="card hoverable">
<div class="card-image">
<img src="http://via.placeholder.com/460x230">
<span class="card-title">{{$post->title}}</span>
<a class="btn-floating halfway-fab waves-effect waves-light red hover"><i class="material-icons">remove_red_eye</i></a>
</div>
<div class="card-content">
<p>
Created by {{$post->user->name}}<br>
<small>on {{$post->created_at->toDateString()}} at {{$post->created_at->toTimeString()}}</small>
</p>
</div>
</div>
</a>
</div>
#endforeach
</div>
#else
<div class="row">
<div class="col s12">
<div class="card">
<p>No posts found</p>
</div>
</div>
</div>
#endif
I think it has something to do with the foreach, but I'm not certain.
Note: There is probably a far better way of doing this, but I'm currently learning Laravel after teaching myself PHP, so it's not the end of the world.
You're getting the problem because you're not closing off the directives properly. Try something like this.
#if(count($posts) > 0)
<div class="row">
#foreach($posts as $post)
<div class="#if (count($posts) === 1) col s12 m6 offset-m3 #elseif (count($posts) === 2) col s12 m6 #else col s12 m4 #endif">
<a href="posts/{{$post->id}}">
<div class="card hoverable">
<div class="card-image">
<img src="http://via.placeholder.com/460x230">
<span class="card-title">{{$post->title}}</span>
<a class="btn-floating halfway-fab waves-effect waves-light red hover"><i
class="material-icons">remove_red_eye</i></a>
</div>
<div class="card-content">
<p>
Created by {{$post->user->name}}<br>
<small>on {{$post->created_at->toDateString()}}
at {{$post->created_at->toTimeString()}}</small>
</p>
</div>
</div>
</a>
</div>
#endforeach
</div>
#else
<div class="row">
<div class="col s12">
<div class="card">
<p>No posts found</p>
</div>
</div>
</div>
#endif
You need to #endforeach
#foreach($posts as $post)
....
#endforeach
The error is saying that there is an unexpected #elseif because the parser expects you to end the foreach before.
you can try this:
#if (count($posts) === 1)
<?php $class = 'col s12 m6 offset-m3'; ?>
#elseif (count($posts) === 2)
<?php $class = 'col s12 m6'; ?>
#else
<?php $class = 'col s12 m4'; ?>
#endif
#foreach($posts as $post)
<div class="{{ $class }}">
//rest of code...
#endforeach
Yes every directive must be closed properly. Meaning you should have the closing tag #endforeach after every #foreach statement.
If it's difficult. You can try
#php #endphp
I am using a blade template and I was wanting to know within my foreach what the best way is for me to have it so that my first .panel-heading has another class added to it and then the second one has another etc
Example:
.red
.blue
.red
.blue
.red
Code:
#foreach ($dealsDB as $deal)
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h4><i class="fa fa-fw fa-gift"></i>{{ $deal->title }}</h4>
</div>
<div class="panel-body">
<p>{{ $deal->content }}</p>
</div>
</div>
</div>
#endforeach
try this
$k=0;
#foreach ($dealsDB as $deal)
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading #if($k%2==0) red #else blue #endif">
<h4><i class="fa fa-fw fa-gift"></i>{{ $deal->title }}</h4>
</div>
<div class="panel-body">
<p>{{ $deal->content }}</p>
</div>
</div>
</div>
$k++;
#endforeach
You can use #switch inside your #foreach to assign different class on every iteration.
#foreach (...)
<div class="
#switch($loop->iteration)
#case(1)
red
#break
#case(2)
green
#break
#case(3)
blue
#break
#default
yellow
#endswitch
">
#endforeach
In Laravel 5.8+, the $loop->odd or $loop->even variables can be used for this.
Furthermore, in Laravel 8.0 the #class directive was added. This is the perfect use case example for it.
In Laravel 5.3+ there is the $loop->iteration which returns the current loop iteration (starts at 1).
Docs:
https://laravel.com/docs/5.8/blade#the-loop-variable
https://laravel.com/docs/8.x/blade#conditional-classes
The following examples will return the exact same result.
Laravel 8+ version
#foreach ($dealsDB as $deal)
<div class="col-md-4">
<div #class([
'panel panel-default',
'red-class' => $loop->odd,
'blue-class' => $loop->even
])>
<div class="panel-heading">
<h4><i class="fa fa-fw fa-gift"></i>{{ $deal->title }}</h4>
</div>
<div class="panel-body">
<p>{{ $deal->content }}</p>
</div>
</div>
</div>
#endforeach
Laravel 5.8+ Version
#foreach ($dealsDB as $deal)
<div class="col-md-4">
<div class="panel panel-default
#if($loop->odd) red-class #else blue-class #endif">
<div class="panel-heading">
<h4><i class="fa fa-fw fa-gift"></i>{{ $deal->title }}</h4>
</div>
<div class="panel-body">
<p>{{ $deal->content }}</p>
</div>
</div>
</div>
#endforeach
Laravel 5.3+ Version
#foreach ($dealsDB as $deal)
<div class="col-md-4">
<div class="panel panel-default
#if($loop->iteration % 2 == 0) blue-class #else red-class #endif">
<div class="panel-heading">
<h4><i class="fa fa-fw fa-gift"></i>{{ $deal->title }}</h4>
</div>
<div class="panel-body">
<p>{{ $deal->content }}</p>
</div>
</div>
</div>
#endforeach
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>
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.