Displaying posts in custom formation - laravel

I would like to output posts in order provided by this image (every 3 posts)
here is my blade code:
<section class="blog_area p_120">
<div class="container">
<div class="row">
<div class="col-lg-8">
<div class="blog_left_sidebar">
#foreach ($raksti as $raksts)
<article class="blog_style1">
<div class="blog_img">
<img class="img-fluid" src="{{$raksts->image}}" alt="">
</div>
<div class="blog_text">
<div class="blog_text_inner">
<div class="cat">
<a class="cat_btn" href="#">{{$raksts->kato->title}}</a>
<i class="fa fa-calendar" aria-hidden="true"></i> {{$raksts->created_at}}
<i class="fa fa-comments-o" aria-hidden="true"></i> 05
</div>
<a href="#">
<h4>{{$raksts->title}}</h4>
</a>
<p>{{$raksts->short_desc}}</p>
<a class="blog_btn" href="#">Lasīt vairāk</a>
</div>
</div>
</article>
#endforeach
</div>
</div>
</div>
</div>
</section>
For those small blocks the "article" tag have class ="blog_style1 small"
I guess that there need to work with a "for" loop, so can anyone help me to achieve this task and explain a little how that works?

Take a look, you need to tell your code that one image of three has to be wide.
So that you can use modulo:
#foreach ($raksti as $key => $value)
#if($key % 3 = 0)
// set width 100%
#else
// set width 50%
#endif
#endforeach
That makes every third element as 100% wide.

try next code
#foreach ($raksti as $key => $raksts)
//
#if($key % 3 == 1)
// set here style css width 100 %
#else
// set here style css width 50 %
#endif
#endforeach

Laravel has loop variables:
https://laravel.com/docs/5.8/blade#the-loop-variable
You can use $loop->index inside yor foreach and check whether your article must have class small
<article class="blog_style1 #if($loop->index % 3 !== 0) small #endif">
Or you can simply use CSS nth-child
https://css-tricks.com/how-nth-child-works/

Give class small each third element
<section class="blog_area p_120">
<div class="container">
<div class="row">
<div class="col-lg-8">
<div class="blog_left_sidebar">
#foreach ($raksti as $key => $raksts)
<article class="blog_style1 {{ ($key % 3 != 0) ? 'small' : '' }}">
<div class="blog_img">
<img class="img-fluid" src="{{$raksts->image}}" alt="">
</div>
<div class="blog_text">
<div class="blog_text_inner">
<div class="cat">
<a class="cat_btn" href="#">{{$raksts->kato->title}}</a>
<i class="fa fa-calendar" aria-hidden="true"></i> {{$raksts->created_at}}
<i class="fa fa-comments-o" aria-hidden="true"></i> 05
</div>
<a href="#">
<h4>{{$raksts->title}}</h4>
</a>
<p>{{$raksts->short_desc}}</p>
<a class="blog_btn" href="#">Lasīt vairāk</a>
</div>
</div>
</article>
#endforeach
</div>
</div>
</div>
</div>
</section>
Hope this helps you

no need to do this in the backend. Use the nth-child functionality of CSS:
.blog_style1{
width: 50%;
}
.blog_style1:nth-child(4n){
width: 100%;
}

Related

initialData is null in livewire component when there is a two events in the same component

I am having two events in my component one is changing the layout and another one is sorting. Changing a layout basically it only replace a layout component inside a parent component. Now when i am calling a sorting then livewire giving me a error 'initialdata is null '.
I am looking for to create a sorting order with this same component. And my code is
<div class="dropdown px-2 d-none d-lg-block">
<div class=" text-dark fw-bold dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<span class="px-1">
<img class="" src="{{ Vite::image('icons/classic.png') }}" alt="" />
</span>
<span>
<img class="" src="{{ Vite::image('icons/filter-down.png') }}" alt="" />
</span>
</div>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item text-primary #if($show == 1)text-primary #else text-dark #endif fw-bolder" href="javascript:void(0)" wire:click="$set('show', '1')">
<img src="{{ Vite::image('icons/compact.png') }}" alt="" class="ps-2" /> Card </a>
<a class="dropdown-item uni-border #if($show == 2)text-primary #else text-dark #endif fw-bolder " href="javascript:void(0)" wire:click="$set('show', '2')">
<img src="{{ Vite::image('icons/classic.png') }}" alt`your text`="" class="ps-2" />Classic </a>
<a class="dropdown-item uni-border #if($show == 3) text-primary #else text-dark #endif fw-bolder" href="javascript:void(0)" wire:click="$set('show', '3')">
<img src="{{ Vite::image('icons/com.png') }}" alt="" class="ps-2" />Compact </a>
</div>
</div>
{{-- sorting option --}}
<div class="d-flex justify-content-between pt-2 align-items-center d-none d-lg-block ">
<div class="d-flex align-items-center justify-content-between">
<div>
<span class="m-0">Selected Filters:</span>
<span class=" mx-3 boat-span2 align-items-center rounded-1">
<span>$500 </span>
<span>
<img src="{{ Vite::image('icons/cross.png') }}" alt="" class="" />
</span>
</span>
<span class=" boat-span2 align-items-center rounded-1">
<span> Skipper </span>
<span>
<img src="{{ Vite::image('icons/cross.png') }}" alt="" class="" />
</span>
</span>
</div>
<div class="">
<h6 class="m-0 text-secondary fw-500 ">
<u> Clear Filters</u>
</h6>
</div>
</div>
</div>
#if($show == 2)
{{-- classic layout --}}
#livewire('frontend.boat.listing.layout.classic',['product' => $product])
#elseif($show == 1)
{{-- card layout --}}
#livewire('frontend.boat.listing.layout.card',['product' => $product])
#elseif($show == 3)
{{-- compact layout --}}
#livewire('frontend.boat.listing.layout.compact',['product' => $product])
#endif
</div>
<?php
namespace App\Http\Livewire\Frontend\Boat\Listing;
use App\Models\Product;
use Livewire\Component;
class Listing extends Component
{
// Mount , boot , hydrate component
public $filter_option_recommend = "recommended";
public $show = "2";
// public $filter_option = "name";
// public $product;
public function detail($id, $title)
{
return redirect()->route('website.boat.detail', [$id, $title]);
}
public function render()
{
$product = Product::join('product_time_assign', 'product_time_assign.product_id', '=', 'products.id')
->where([
'products.type_id' => 1, // boat
'products.status' => 'Active',
])
->select(
'products.id as productid',
'product_time_assign.id as timeassignid',
'products.name',
'products.capacity',
'products.price_starts',
'product_time_assign.skipper',
'product_time_assign.product_time_assign_id',
'products.review_rating',
'products.image',
'products.description',
'products.discount_price'
)
->orderBy($this->filter_option_recommend, 'asc')->get();
return view('livewire.frontend.boat.listing.listing', ['product' => $product]);
}
}

Unable to show category wise data in sections of home page

I want to fetch category wise data in home page's sections but I am unable to do this. Only one category section is working[enter image description here][1]
I made Section model and under this model Category model My IndexController Code :
<?php
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Slider;
use App\Category;
use App\Section;
class IndexController extends Controller
{
public function index(){
$sliders = Slider::where('status',1)->get();
$categories = Category::with('sections')->where('status',1)->get();
//dd($categories);die;
return view('frontend.index')->with(compact('sliders','categories'));
}
}
and blade file code is :
<section class="latest_work">
<div class="container">
<div class="row sub_content">
<div class="col-md-12">
<div class="dividerHeading">
<h4><span>Find your best tours</span></h4>
</div>
<div id="recent-work-slider" class="owl-carousel">
#foreach($categories as $category)
#if($category['sections']['section_name'] == "Tours")
<div class="product">
<figure class="touching effect-bubba">
<div class="product-img">
<img class="img-responsive" src="{{ asset('images/categories/'.$category['category_banner']) }}">
<div class="option">
<a class="fa fa-shopping-cart" href="portfolio_single.html"></a>
<a class="fa fa-search mfp-image" href="{{ asset('images/categories/'.$category['category_banner']) }}"></a>
</div>
</div>
</figure>
<div class="product-info">
<div class="product-title">
<h3>
{{ $category['category_name'] }}
</h3>
</div><br>
</div>
</div>
#endif
#endforeach
</div>
</div>
</div>
</div>
</section>
<section class="latest_work">
<div class="container">
<div class="row sub_content">
<div class="col-md-12">
<div class="dividerHeading">
<h4><span>Find your best stays</span></h4>
</div>
<div id="recent-work-slider" class="owl-carousel">
#foreach($categories as $category)
#if($category['sections']['section_name'] == "Stays")
<div class="product">
<figure class="touching effect-bubba">
<div class="product-img">
<img class="img-responsive" src="{{ asset('images/categories/'.$category['category_banner']) }}">
<div class="option">
<a class="fa fa-shopping-cart" href="portfolio_single.html"></a>
<a class="fa fa-search mfp-image" href="{{ asset('images/categories/'.$category['category_banner']) }}"></a>
</div>
</div>
</figure>
<div class="product-info">
<div class="product-title">
<h3>
{{ $category['category_name'] }}
</h3>
</div><br>
</div>
</div>
#endif
#endforeach
</div>
</div>
</div>
</div>
</section>
<section class="latest_work">
<div class="container">
<div class="row sub_content">
<div class="col-md-12">
<div class="dividerHeading">
<h4><span>Find your best cars</span></h4>
</div>
<div id="recent-work-slider" class="owl-carousel">
#foreach($categories as $category)
#if($category['sections']['section_name'] == "Cars")
<div class="product">
<figure class="touching effect-bubba">
<div class="product-img">
<img class="img-responsive" src="{{ asset('images/categories/'.$category['category_banner']) }}">
<div class="option">
<a class="fa fa-shopping-cart" href="portfolio_single.html"></a>
<a class="fa fa-search mfp-image" href="{{ asset('images/categories/'.$category['category_banner']) }}"></a>
</div>
</div>
</figure>
<div class="product-info">
<div class="product-title">
<h3>
{{ $category['category_name'] }}
</h3>
</div><br>
</div>
</div>
#endif
#endforeach
</div>
</div>
</div>
</div>
</section>
only one tours section's categories is showing but not showing other categories
Yes Only one tour sections is showing because of this condition in your code
#if($category['sections']['section_name'] == "Tours")
........................
#endif

How not to show image if there is none?

I have three table. Category, PostAd and PostImage.
To show post i have to show it from category table.
My controller Code.
$data['category'] = Category::with(['child','children','parent','postads.postimage','postads'=>function($q) use ($asc){
$q->orderBy('created_at',$asc);
}])->where('id',$id)->get();
To display image i have to use nested relationship in postads.postimage.
Blade Code
#foreach($category as $cat)
#foreach($cat->postads as $c)
<a href="{{route('particular',['id'=>$c->id])}}">
<li>
#foreach($c->postimage as $pi)
<img src="{{asset('thumbnail/'.$pi->image)}}" alt="No image" style="margin-top: 5px" >
#endforeach
<section class="list-left">
<h5 class="title">{{$c->adtitle}}</h5>
<span class="adprice">Rs. {{$c->price}}</span>
<p class="catpath">{{$cat->categoryname}} » {{$cat->categoryname}}</p>
</section>
<section class="list-right">
#auth
<div class="like1">
<i class="fas fa-heart" pid="{{$c->id}}" uid="{{auth()->user()->id}}"></i>
</div>
#endauth
<span class="date">{{date('D',strtotime($c->created_at))}}-{{date('M',strtotime($c->created_at))}}-{{date('Y',strtotime($c->created_at))}}</span>
<span class="cityname">{{$c->address}}</span>
</section>
<div class="clearfix"></div>
</li>
</a>
#endforeach
#endforeach
because of foreach loop my design gets damaged.
I want to show image alt but because of this nothing is shown and everything gets damaged.
You can use #forelse
#forelse($c->postimage as $pi)
<img src="{{asset('thumbnail/'.$pi->image)}}" alt="No image" style="margin-top: 5px" >
#empty
<!-- some HTML, default image or something, whatever you need -->
#endforelse
https://laravel.com/docs/5.8/blade#loops

laravel blade limiting the showing images proplem

everyone, I have a problem in laravel blade I have a page for my website that show all the image because my website is a sale website but when I load the page it shows all the images but I don't want to show all the images I want to hide a images after three or four columns if someone can help me please.
thanks for everyone.
this is my blade code:
<section class="container custom-margin ">
<h5 class="text-center" style="text-shadow: 1px 1px 2px black; color: #1b1e21">مجموعه ما با ۲۰۰۰ خوابگا در سطح کشور</h5>
<div class="justify-content-center thin-underline-1"></div>
<div class="row justify-content-center">
#foreach($hostels as $hostel)
#foreach($hostel->hostelDetails->attachments as $photo)
<div class=" col-12 col-sm-6 col-md-6 col-lg-3 px-1 mt-4 small-device-hid ">
<div class="card card-shadow custom-height-1 " style="border-radius: 0%">
<a href="{{route('khabgah_detailes.goToDetails',$hostel->id)}}"> <img src="/images/{{ $photo->file_name }}"
class="card-img-top card-img custom-card-img-height" alt=""></a>
<div class="car-body">
<div class="card-footer">
<div class="custom-circle"><p class="custom-circle-text card-text"><b>
#if($hostel->type == 1)
{{ 'ذکور'}}
#else {{ 'اناث' }}
#endif
</b></p></div>
<div class="custom-prices card-text text-left"> کرایه فی ماه : {{$hostel->hostelDetails->remark }} </div>
<div class="row mt-3">
<div class="col-12 col-sm-12 col-md-12 mb-2 ">
<span class="card-text">آدرس : {{$hostel->addresses->state }} {{$hostel->addresses->rood}}
{{$hostel->addresses->station }} {{$hostel->addresses->alley}}
</span>
</div>
</div>
</div>
</div>
</div>
</div>
#endforeach
#endforeach
</div>
</section>
Since every Laravel relation return a collection instance, you could as well use the take method to obtain the first N objects in your collection.
For example:
#foreach($hostel->hostelDetails->attachments->take(3) as $photos)
Another way would be to use the query builder of the attachments relation to limit the results at query level:
#foreach($hostel->hostelDetails->attachments()->limit(3)->get() as $photos)
The latter way would love preferred performance-wise as it would only fetch the needed models from the database. The former would fetch all the models from the database and would only display a few of them.

Need to give row class after each row in laravel 5.2

I am trying to display 3 results in each row. 1 result contains image,title and description. If description is a lengthy one, the 4th result in the second row will break. So I need to give after each row. I tried the following code, but didn't work.Thanks in advance.
<div class="row">
#if(count($reports['data']) > 0)
#foreach($reports['data'] as $reportsUser)
<div class="col-md-4 wow">
<article class="post post-2 post-2_mod-c clearfix">
<div class="entry-media">
#if($reportsUser['image'])
<img src="{{ asset(App\Http\Controllers\Controller::getThumbPath($reportsUser['image'])) }}"
alt="Foto" class="img-responsive" style="width:360px;height:192px;"/>
#else
<img src="{{asset('images/no_image.jpg')}}" alt="Foto" class="img-responsive"/>
#endif
</div>
<div class="entry-main">
<div class="entry-header">
<h2 class="entry-title ">{{$reportsUser['title']}}</h2>
</div>
<div class="entry-content">
<p>{!! str_limit($reportsUser['description'],127,"....") !!}</p>
</div>
<div class="entry-footer"><a href="{{ url('view-report/'.$reportsUser['id'])}}"
class="btn-link clsComClrRed">Continue Reading</a></div>
</div>
</article>
</div>
#endforeach
#endif
</div>
Change your #foreach statement to:
#foreach($reports['data'] as $i => $reportsUser)
Then, just before your first #endforeach (and after </div>), add:
#if ( $i % 3 == 2 )
<div class="clearfix"></div>
#endif

Resources