modulus with remainder laravel - laravel

I am trying to setup columns of 4 from a laravel array which works correctly. The problem is how do you handle remainders? With my loop I end up with 2 extra divs that are empty at the end of my output. I have 10 items that i'm looping over which would give me a remainder of 2.5 Here is my code.
<div class="serviceListingColumn">
<ul>
#foreach($serviceListings as $serviceListing)
#if ($serviceListing->service_category == $services->title)
<li class="serviceListingItem">{{$serviceListing->service_name}}</li>
#endif
#if($loop->iteration % 4 === 0)
</ul>
</div>
<div class="serviceListingColumn">
<ul>
#endif
#endforeach
</ul>

I would suggest chunking the original array into groups, then just looping those. Easier for the template logic.
// Use the Collection's group() functionality in your controller.
// Use collect() if it isn't a Collection already.
$array = collect($array)->chunk(4);
// Your template then doesn't need to worry about modulus,
// and can focus on displaying the chunked groups.
#foreach ($array as $group)
<div class="serviceListingColumn">
<ul>
#foreach ($group as $serviceListing)
<li class="serviceListingItem">{{ $serviceListing->service_name }}</li>
#endforeach
</ul>
</div>
#endforeach

Related

Laravel - Element displaying twice while looping inside a condition

This is the example while looping
despite having a condition and relation many too many it seems that the case
#foreach (\App\ClientDocument::where('id_client', $clients->id)->get() as $item)
#if (\App\Document::where('id',$item->id_document)->get())
<a class="img-fluid " href="{{asset('images/'.$item->file)}}" download="{{asset('images/'.$item->file)}}" alt="" name="download" > Telecharger </a>
#endif
#endforeach
in the above snippet
#foreach (\App\ClientDocument::where('id_client', $clients->id)->get() as $item)
#if (\App\Document::where('id',$item->id_document)->get())
<a class="img-fluid " href="{{asset('images/'.$item->file)}}" download="{{asset('images/'.$item->file)}}" alt="" name="download" > Telecharger </a>
#endif
#endforeach
code is absolutely fine, but there might be the possibility, you are running with duplicate data, you can use this also.
\App\ClientDocument::where('id_client', $clients->id)->distinct('id_client')->get()
and instead of using #if (\App\Document::where('id',$item->id_document)->get())
you can use
#if (\App\Document::where('id',$item->id_document)->firstOrFail())
#if (\App\Document::where('id',$item->id_document)->count())
That's it.
first you shouldn't loading data inside blade files
do it inside controller function
then you should create relationship inside ClientDocument model name document
and in your blade you can do that
$item->document
or whatever you want

How can I filter the data of foreach?

I have this design:
I need to say: last post that I add it put it in the head of design then other put them down.
My shut :) Html and foreach code:
#if ($user->projects->count() > 0)
<section class="latest section shadow-sm">
<h1>Projects</h1>
<div class="section-inner">
#foreach ($user->projects->sortByDesc('id')->take(1) as $project)
<div class="item featured text-center ">
// head post
</div>
#endforeach
#foreach ($projects_last->sortByDesc('id') as $project)
<div class="item row">
// other post
</div>
#endforeach
</div><!--//section-inner-->
</section><!--//section-->
#endif
Code of controller for $projects_last:
$projects_last = $user->projects;
$projects_last->pop();
return view('frontend.user_profile',compact('user','projects_last'));
I have the problem with when I say if the #if ($user->projects->count() > 0) do not show any thing but still show me the <h1>Projects</h1> even it is empty!
And if you have any suggest to making my code better pls do it with thankful :)
To iterate Collections you have to get them. So you have to use get() to get the results.
...
#foreach ($user->projects->sortByDesc('id')->take(1)->get() as $project)
...
and
...
#foreach ($projects_last->sortByDesc('id')->get() as $project)
...
You can see here the documentation: Laravel query documentation
Note: if you want to get just one element in your first foreach loop you can use first() instead of take(1). You code will be like that:
#php($first_project = $user->projects->sortByDesc('id')->first())
#if (!is_null($first_project))
// Use $first_project as $project variable
#enif

Laravel show all links in paginator

Since Laravel 5.7, the paginator links() method shows only a reduced number of pages when called. The number of links can be customized using the onEachSide($count) method.
‹‹ ‹ 1 2 ... 47 48 49 50 51 52 53 ... 102 103 › ››
I want to display every page in order to make a page selector dropdown, using a html <select>.
Calling onEachSide(9999) does work, but it looks flimsy, and may break on high page counts that the app i'm writing could reach.
What would be the clean way to get all the page links ?
EDIT: I am using a custom view, which looks like the following :
#if ($paginator->hasPages())
<ul class="pagination">
{{-- Previous Page Link --}}
#if ($paginator->onFirstPage())
<li class="disabled">‹‹</li>
<li class="disabled">‹</li>
#else
<li>‹‹</li>
<li>‹</li>
#endif
{{--Pagination Elements --}}
#foreach ($elements as $element)
#if (is_string($element))
<li class="disabled"><span>{{ $element }}</span></li>
#endif
#if (is_array($element))
#foreach ($element as $page => $url)
#if ($page == $paginator->currentPage())
<li class="active"><span>{{ $page }}</span></li>
#else
<li>{{ $page }}</li>
#endif
#endforeach
#endif
#endforeach
{{-- Next Page Link --}}
#if ($paginator->hasMorePages())
<li>›</li>
<li>››</li>
#else
<li class="disabled">›</li>
<li class="disabled"><span>››</li>
#endif
</ul>
#endif
The $elements variable seems to already have the extra pages removed, however.
The paginator uses the bootstrap-4 blade view to render the page. This view always implements the tripple dots.
You could create a custom view that renders all pagination pages without any tripple dot items and pass this view as the first parameter to the links function.
Update: The view you are using is exactly the same as the one build in to Laravel. You shouldn't use the $element variable as this gets windowed with tripple dot elements.
You should use the pagination lastpage variable and loop over all pages yourself. For example, like this:
{{-- Pagination Elements --}}
#for ($page = 1; $page <= $paginator->lastPage(); $page++)
#if ($page == $paginator->currentPage())
<li class="active"><span>{{ $page }}</span></li>
#else
<li>{{ $page }}</li>
#endif
#endforeach

How to show comments and replycomments in laravel

I don't know how to show comments and replycomments in my post page. My comments table contains id, sender_id, replyer_id, reply_id, comment_text.
CommentController returns a comment object. In Post.blade.php, how do I write a foreach loop or loops?
Your Controller code must be similar to this:
public function index()
{
$comments = Comment::with(['sender', 'other-relation'])->get();
return view('comments.index', compact('comments'));
}
and your blade code must be similar to this:
<ul>
#foreach($comments as $comment)
<li>{{ $comment->comment_text }}</li>
#if ($comment->sender) // or other relation
<a> {{$comment->sender->name}}<a> // relation name and column name must be fix yourself
#endif
#endforeach
</ul>
Inside blade file:
<ul>
#foreach($comments as $comment)
<li>{{ $comment->comment_text }}</li>
#endforeach
</ul>

I want to count in if statement in foreach loop laravel 5.6

As I am new to Laravel so facing this problem and tried it in other ways but it is not working.
Here my blade file
products.blade.php
#foreach($products as $product)
<div class="women">
<h6>{{$product->title}}</h6>
<span class="size">XL / XXL / S </span>
<p ><em class="item_price">Rs.{{$product->price}}</em></p>
</div>
#if(count($product) == 3)
<div class="clearfix"></div>
#endif
#endforeach
Why this is not working
#if(count($product) == 3)
<div class="clearfix"></div>
#endif
Or how can I count the product in iteration and compare the count number in if statement?
You can use loop variable like this.
Instead of:
#if(count($product) == 3)
<div class="clearfix"></div>
#endif
you should use:
#if($loop->iteration == 3)
<div class="clearfix"></div>
#endif
But it's quite possible that you might need it after every 3 elements (3, 6, 9 and so on), so maybe better solution would be:
#if($loop->iteration % 3 == 0)
<div class="clearfix"></div>
#endif
You example didn't work because $product is just an object so count($product) will not have expected value. Also if you used count($products) (notice trailing s) it won't work because count of products is the same in each loop iteration.
If you have more or less than 3 products, your if statement is never going to show. What you're looking for is the third item in the products array, which you can do like this:
#foreach($products as $key => $product)
<div class="women">
<h6>{{$product->title}}</h6>
<span class="size">XL / XXL / S </span>
<p><em class="item_price">Rs.{{$product->price}}</em></p>
</div>
#if($key == 2)
<div class="clearfix"></div>
#endif
#endforeach
To get the index of the loop, use
#foreach ($athletes as $key=>$athlete)
// Some html goes here
{ { ++$key } }
#endforeach
add if condition to key
let me know how it goes
The array count will always be the same, inside or outside of the loop.
If you want to make a decision based on the current iteration, ex. "on every third product put a clearfix div", then apply the condition to the key if the key is numeric.
Blade provides a loop variable with an iteration property (starts at 1) to help with this, see here https://laravel.com/docs/5.6/blade#the-loop-variable
Example for every third product:
#foreach($products as $product)
...
#if ($loop->$loop->iteration%3 == 0)
<div class="clearfix"></div>
#endif
...
#endforeach
Example for the third product only:
#if ($loop->$loop->iteration == 3)
<div class="clearfix"></div>
#endif

Resources