I have a laravel application which is an ecommerce store.
On my shop category which contains rows of products I'd like to randomly in between the products insert a div.
This div should be random throughout the page.
For example, I have the following (I've pruned the code to keep it clean):
#section('content')
<div class="products">
#foreach($page->products->chunk(6) as $chunk)
<div class="group">
#foreach($chunk as $product)
<div class="category_product">
<div class="category_product_info">
<h2>
<a href="{{ $product->slug->slug }}">
{{ $product->title }}
</a>
</h2>
</div>
</div>
#endforeach
</div>
#endforeach
</div>
#endsection
In the chunk of 6 is it possible to inject say a seventh element which would appear randomly on the row?
I don't see what information you are trying to add into your 7th div, but here is how I see it: check for each loop if a random between 1 and 100 it higher than your resired random rate ( in my case 50% ) , if so add the div.
#section('content')
<div class="products">
#foreach($page->products->chunk(6) as $chunk)
<div class="group">
#foreach($chunk as $product)
<div class="category_product">
<div class="category_product_info">
<h2>
<a href="{{ $product->slug->slug }}">
{{ $product->title }}
</a>
</h2>
</div>
</div>
#endforeach
</div>
#if (rand(1, 100) > 50)
<div class=randomdiv></div>
#endif
#endforeach
</div>
#endsection
You would apply the same process with your chuck, instead of 6 , add a one line if ( i don't know how they are called ). something like #foreach($page->products->chunk(rand(1, 100) > 50 ? 6 : 7) as $chunk)
Related
I have this:
#foreach($portfolios as $portfolio)
<div id="item-portfolio-expand{{$portfolio->id}}" class="item-portfolio-expand expand">
<div class="container">
<div class="row">
<div class="col-md-8">
col-md-8
</div>
<div class="col-md-4">
{{ __('Name')}}: {{ $portfolio->name }}
</div>
</div>
</div>
</div>
#endforeach
And this:
#foreach ($portfolios as $portfolio)
<div class="portfolio-item {{ $portfolio->tag }} " data-category="transition">
<img src="{{ $portfolio->image_show }}">
<a href="#item-portfolio-expand{{$portfolio->id}}" data-toggle="collapse" data-target="#item-portfolio-expand{{$portfolio->id}}" data-parent="#item-portfolio-expand{{$portfolio->id}}">
<div class="portfolio-item-overlay">
<p class="name-item-overlay">{{ $portfolio->name }}</p>
</div>
</a>
</div>
#endforeach
And when I press the in the first foreach, I want to toggle the second foreach at <div id = "item-portfolio-expand {{$ portfolio-> id}}" class = "item- portfolio-expand expand "> I just don't succeed at anything, I use bootstrap and jquery in the project. .item-portfolio-expand has display: none;
And at the same time when one is open and another is selected, to close the one already open and to appear the one selected with toggle.
In my Laravel application, I have implemented Tagging of articles and events. This is accomplished by having two tables: tags and taggables. In my tag controller, I have an index method that grabs all the tags, a count of the tags and the same logic to count used tags.
It looks like this:
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$tags = Tag::orderByDesc('name')->get();
$tagCount = count($tags);
$usedTags = Tag::has('articles')->orHas('events')->orderBy('name')->get();
$usedTagsCount = count($usedTags);
return view('pages.tags.index', compact('tags', 'usedTags', 'tagCount', 'usedTagsCount'));
}
I pass all this data to my view which looks like this:
<div class="container">
<div class="row">
<!-- Search results -->
<div class="col-md-8">
<div class="padded-content-box">
<div class="header">
<div class="row">
<div class="col-xs-12 col-md-12">
<h1 class="heading">Tags</h1>
</div>
</div>
</div>
</div>
<div class="padded-content-box">
<div class="header">
<div class="row">
<div class="col-xs-12 col-md-12">
<h2 class="sub-heading">
All Tags ({{ $tagCount }})
</h2>
</div>
</div>
</div>
<div class="content">
<div class="tag-row">
<div class="tag-words-list">
#foreach($tags as $tag)
<a class="tag-word" title="{{ $tag->name }}" href="{{ URL::action('TagController#show', $tag->slug) }}">{{ $tag->name }}</a>
#endforeach
</div>
</div>
</div>
</div>
<div class="padded-content-box">
<div class="header">
<div class="row">
<div class="col-xs-12 col-md-12">
<h2 class="sub-heading">
Tags currently in use ({{ $usedTagsCount }})
</h2>
</div>
</div>
</div>
<div class="content">
#foreach($usedTags as $tag)
<p>
<a class="tag-word" title="{{ $tag->name }}" href="{{ URL::action('TagController#show', $tag->slug) }}">{{ $tag->name }}</a>
x {{ count($tag->articles) + count($tag->events) }}
</p>
#endforeach
</div>
</div>
</div>
<div class="col-md-4"></div>
</div>
</div>
As you can see, to show a count of tags used I just add the articles and events that have a given tag.
How would I count how many times each tag is used in both events and articles, then order them by the count. Essentially if a tag has been used the most it would be at the top of a list.
Update
As suggested I have changed my controller method to look like this:
public function index()
{
$tags = Tag::orderBy('name')->get();
$tagCount = count($tags);
$usedTags = Tag::has('articles')->orHas('events')->withCount('articles', 'events')->orderByRaw('articles_count + events_count DESC')->orderBy('name')->get();
$usedTagsCount = count($usedTags);
return view('pages.tags.index', compact('tags', 'usedTags', 'tagCount', 'usedTagsCount'));
}
This ensures that the tags are in count and alphabetical order.
Then in my view I changed the count code to this:
<p>
<a class="tag-word" title="{{ $tag->name }}" href="{{ URL::action('TagController#show', $tag->slug) }}">{{ $tag->name }}</a> x {{ $tag->articles_count + $tag->events_count }}
</p>
This way I get to remove some minor logic from the view.
Use withCount():
$tags = Tag::withCount('articles', 'events')
->orderByRaw('articles_count + events_count DESC')
->get();
I have a basic problem. in this foreach loop I would like to add a class of active for the very first eloquent record not for all.
#foreach($products as $product)
<div class="container /*firstrecord*/">
<h1>{{ $product->title }}</h1>
</div>
#endforeach
output:
<div class="container active">
<h1>Title 1</h1>
</div>
<div class="container">
<h1>Title 2</h1>
</div>
So for the first record gets active. Would I have to set this here or in the model for product? Seems like there is an easy way but I am just missing it. Apologies
You can access to the index like this:
#foreach($products as $i => $product)
<div class="container #if($i == 0) active #endif">
<h1>{{ $product->title }}</h1>
</div>
#endforeach
I'm trying to make a container with a list of images and attributes, but when I add a tag around the array, the array simply go outside my container, I had searched this type of question but didn't find it.
This is the code:
#if(count($elitePublications) > 0)
<div style="border:2px gold solid">
#forelse($elitePublications as $key => $publication)
#include('publications.small', compact('publication'))
#empty
<p></p>
#endforelse
</div>
#endif
It only make the container but the list is outside
The code of publications.small:
<div class="col-sm-4 book">
<a href="{{ route('users.publications.show', ['publications'=>$publication->id, 'users'=>$publication->user->username]) }}">
<div class="book-cover">
{{ $publication->image }}
<div class="inner-book-content">
<h4>{{ $publication->published_book_title }}</h4>
<strong class="book-price">
</strong>
</div>
</div>
</a>
</div>
Thanks in advance.
The issue here is about Bootstrap. Some kind of problem with the columns and the rows (who guest the results), so I just added a class in my div called "row" it act as a container for my array.
Like:
<div class="row">
<h4 style="font-style: italic;"> Recomendados </h4>
#forelse($elitePublications as $key => $publication)
#include('publications.small', compact('publication'))
#empty
#endforelse
</div>
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>