Create bootstrap columns with Laravel and foreach - laravel

I want the items of my $favs object to be grouped in 4 columns for each row
<div class="row row justify-content-center">
<div class="col-9">
<div class="row">
#foreach ($favs->items as $fav)
<ul class="col-sm-3 list-unstyled">
<li class="subcat-li">{{ $fav->name }}</li>
</ul>
#endforeach
</div>
</div>
</div>
I want it to look like this:

<div class="row">
#foreach($favs->items as $fav)
<div class="col-md-3">
{{ $fav->name }}
</div>
#endforeach
</div>

Related

Foreach loop returns all results

I have a column of album covers. I want to take me to the album details after click on the cover. Actually it takes me to the album details but it shows all results, not only this one which was clicked.
AlbumDetailsController
public function index(): View
{
return View("details.index", [
'albums' => Album::paginate(1)
]);
}
AlbumDetails Blade view
#extends('layouts.app')
#section('content')
<div class="container">
#foreach ($albums as $album)
<h1 class="my-4">{{ $album->name }}</h1>
<div class="row">
<div class="col-lg-6 mb-4">
<div class="card h-100">
<div class="card-body">
<h4 class="card-title">
</h4>
{{ $album->artist }}
</div>
</div>
</div>
<div class="col-lg-6 mb-4">
<div class="card h-100">
<img src="{{asset('storage/' . $album->image_path)}}" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
{{ $album->artist }}
</h4>
{{ $album->description }}
</div>
</div>
</div>
</div>
#endforeach
#endsection
I've tried to solve this in differents ways but everything is not working. I think problem is with my controller but I'm really don't know how to modify it to show only this item which was clicked.

Dynamic Carousel In Laravel not displaying proper data

<div class="container mt-4 mb-4">
<div class="row">
<div class="col-lg-12">
<div id="blogCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
#foreach($reviews as $items)
<div class="carousel-item #if($loop->first) active #endif">
<div class="row">
#foreach($reviews as $review)
<div class="col-lg-4 ">
<a>
<div class="home1-img mt-3">
<?php
for($i=1;$i<6;$i++){
$check = '';
if($review->number_of_stars >= $i){
$check = 'checked';
}
echo '<span class="fa fa-star '.$check.'"></span>';
}
?>
<div class="home1-text mt-1" style="margin-bottom:30px;">
<p>{!! Illuminate\Support\Str::limit($review->customer_review,100) !!}</p>
<h5 class="text-color font-weight-bold">{{ $review->customer_name }}</h5>
</div>
</div>
</a>
</div>
#endforeach
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
I am getting all the records in all sides but I want 3 records in 1st slide then 3 records in 2nd slide and so on. I have tried many times but I am not able to fix it.

remove duplicate for only title laravel

In laravel 5.7 in my web controller
public function index(Request $request){
$plan = Plan::All();
return view('web.plan.index', compact('plan'));
}
In my view file
#foreach($plan as $singlePlan)
#if(Carbon\Carbon::parse($singlePlan->date)->format('m')==10)
<div class="row brow bg-gray ">
<div class="col-md-12">
<div class="row-title">
<strong>12.31</strong>
</div>
</div>
</div>
<div class="m-departures">
<div class="row brow last-row" >
<div class="col-md-2 col-sm-2 col hidden-xs">
<div class="td center">Dec 21-22</div>
</div>
<div class="col-md-5 col-sm-5 col">
<div class="td">Fest</div>
</div>
<div class="col-xs-6 col visible-xs">
<div class="td">2 day</div>
</div>
<div class="col-md-1 col-sm-2 col hidden-xs">
<div class="td">2 day</div>
</div>
<div class="col-md-2 col-sm-3 col-xs-6 col">
<div class="td">
<span class="orange schedule-status">
4
</span>
</div>
</div>
<div class="col-md-2 col-xs-12 col tour-link hidden-sm hidden-xs">
<div class="td center"><a href="#" >view</a></div>
</div>
</div>
</div>
#endif
#endforeach
and my web looks like this
but I want it to look like this
How can I remove the duplicate title?
It would be better to group your data by the month before passing data to blade template. By using collection method mapToGroups
https://laravel.com/docs/8.x/collections#method-maptogroups
$planGroups = Plan::get()->mapToGroups(function($plan, $key) {
$planDate = \Carbon\Carbon::parse($plan->date);
return ["{$planDate->month} {$planDate->year}" => $plan];
})
->all();
You will get several groups of plans grouped by month, e.g.
[
'11 2020' => [plan, plan, ...],
'12 2020' => [plan, plan, plan, ...]
]
Then in your blade template you can loop through the months then loop through the plans.
#foreach ($planGroups as $month => $plans)
// show month heading
#foreach ($plans as $plan)
// show each plan
#endforeach
#endforeach

shoping cart data display in laravel

I want to display the order details form database. I store cart data into database using serialize method. When i want to display data i can not properly show it. only can print order object. And there give that a error.That is
(1/1) FatalErrorException syntax error, unexpected 'endforeach'
(T_ENDFOREACH) in 9ad30ab1db6ca668d8a75c428e65858dc800d045.php line 27
.
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>User Profile</h1>
<hr>
<h2>My Oders</h2>
#foreach($orders as $order)
<div class="panel panel-default">
<div class="panel-body">
<ul class="list-group">
#forecah($order->cart->items as $product)
<li class="list-group-item">
<span class="badge">{{$product['product_price']}}
</span>
{{$product['item']['product_name']}} |
{{$product['qty']}} Units
</li>
#endforeach
</ul>
</div>
<div class="panel-footer">
<strong>Total Price : {{$order->cart->totalPrice}}</strong>
</div>
</div>
#endforeach
</div>
</div>
You have typo in your code:
#forecah($order->cart->items as $product)
Try to change into:
#foreach($order->cart->items as $product)e
Hope it works..as it does not detect the foreach loop before.
You have wrong spelling of forecah. you should correct the spelling.
Look at the second foreach and change the spelling.
You can copy code below as i have changed the spelling.
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>User Profile</h1>
<hr>
<h2>My Oders</h2>
#foreach($orders as $order)
<div class="panel panel-default">
<div class="panel-body">
<ul class="list-group">
#foreach($order->cart->items as $product)
<li class="list-group-item">
<span class="badge">{{$product['product_price']}}
</span>
{{$product['item']['product_name']}} |
{{$product['qty']}} Units
</li>
#endforeach
</ul>
</div>
<div class="panel-footer">
<strong>Total Price : {{$order->cart->totalPrice}}</strong>
</div>
</div>
#endforeach
</div>

Laravel 5: How to get posts by specific categories and display in tabs

I'm building a news site. I want to get 6 latest posts by 3 specific categories, lets say id 2, 3, 4, and display them in 3 tabs on homepage.
Each tab is divided in 2 column, col-6. one col-6 display the latest record, the other display a list of another 5 records after the latest one.
Here's my controller, I get 6 latest records from database
$post_tabs = Post::with('categories')->latest()->limit(6)->get();
this I get 3 categories that have id 2, 3, 4
$category_tabs = Category::with('posts')->whereIn('id', [2, 3, 4])->get();
In blade view, I managed to display that 3 category tabs, like so
<div class="collapse navbar-collapse justify-content-between" id="navbarToggler1">
<ul class="nav nav-sm navbar-nav mr-md-auto mr-lg-0 mt-2 mt-lg-0 align-self-end" role="tablist">
#foreach($category_tabs as $tab_category)
<li class="nav-item">
<a class="nav-link bg-color-tech active" id="nav-outdoor-tab-{{$tab_category->id}}" data-toggle="tab" href="#nav-outdoor-{{$tab_category->id}}" role="tab" aria-selected="true">
{{$tab_category->title}}
</a>
</li>
#endforeach
</ul></div>
Now I don't know how to display posts of those categories by tabs, this is what i 've got so far, which only display 6 latest post and I don't know how to make tabs work either
#foreach($post_tabs as $key => $post_tab)
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-outdoor-{{-- {{$tab_category->id}} --}}" role="tabpanel">
<div class="row clearfix">
<div class="col-lg-6">
#if ($loop->first)
<article>
<div class="entry-image mb-3">
<img src="{{asset($post_tab->thumbnail)}}" alt="{{$post_tab->title}}">
#foreach($post_tab->categories as $category)
<div class="entry-categories">{{$category->title}}</div>
#endforeach
</div>
<div class="entry-title">
<h3><a target="_blank" href="{{route('post.show',[$post_tab->slug])}}">{{$post_tab->title}}</a></h3>
</div>
<div class="entry-content clearfix">
<p>{{$post_tab->description}}</p>
</div>
</article>
</div>
<div class="col-lg-6">
#foreach($post_tabs as $key => $post_tab)
#if($key > 0)
<article>
<div class="entry-image">
<img src="{{asset($post_tab->thumbnail)}}" alt="">
</div>
<div class="entry-c">
<div class="entry-title">
<h4>{{$post_tab->title}}</h4>
</div>
</div>
</article>
#endif
#endforeach
#endif
</div>
</div>
</div>
</div>
#endforeach
Thank you so much.
You don't need to fetch category and post separately. You can fetch both using eager loading. For that you can define extra relationship in Category model like this
Category Model
public function latestPosts(){
return $this->hasMany('App/Post')->latest()->take(6);
}
public function beforeLatestPosts(){
return $this->hasMany('App/Post')->latest()->slice(6)->take(5);
}
Fetch in controller
$category_tabs = Category::with('latestPosts', 'beforeLatestPosts')->whereIn('id', [2, 3, 4])->get();
In view
Print Tab header
<div class="collapse navbar-collapse justify-content-between" id="navbarToggler1">
<ul class="nav nav-sm navbar-nav mr-md-auto mr-lg-0 mt-2 mt-lg-0 align-self-end" role="tablist">
#foreach($category_tabs as $tab_category)
<li class="nav-item">
<a class="nav-link bg-color-tech active" id="nav-outdoor-tab-{{$tab_category->id}}" data-toggle="tab" href="#nav-outdoor-{{$tab_category->id}}" role="tab" aria-selected="true">
{{$tab_category->title}}
</a>
</li>
#endforeach
</ul>
</div>
Print Tab Content
#foreach($category_tabs as $tab_category)
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-outdoor-{{$tab_category->id}}" role="tabpanel">
<div class="row clearfix">
<div class="col-lg-6">
#foreach($tab_category->latestPosts as $latestPost)
<article>
<div class="entry-image mb-3">
<img src="{{asset($latestPost->thumbnail)}}" alt="{{$latestPost->title}}">
</div>
<div class="entry-title">
<h3><a target="_blank" href="{{route('post.show',[$latestPost->slug])}}">{{$latestPost->title}}</a></h3>
</div>
<div class="entry-content clearfix">
<p>{{$latestPost->description}}</p>
</div>
</article>
#endforeach
</div>
<div class="col-lg-6">
#foreach($tab_category->beforeLatestPosts as $beforeLatestPost)
<article>
<div class="entry-image">
<img src="{{asset($beforeLatestPost->thumbnail)}}" alt="">
</div>
<div class="entry-c">
<div class="entry-title">
<h4>{{$beforeLatestPost->title}}</h4>
</div>
</div>
</article>
#endforeach
</div>
</div>
</div>
</div>
#endforeach

Resources