Eloquent Laravel add string to first record found - php

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

Related

what is the way to show category wise product in laravel

controller.php
$shop['product'] = Product::with('ProductImages')->where('product_status','=',1)->where('product_drop_status','=','no')->where('language_code','=',$translate)->orderBy('product_id','desc')->get();
index.blade.php
#foreach($categories['display'] as $category)
<div class="container pt-3 mt-3 pb-3 mb-3">
<div class="row">
<h4 class="black mb-2 pb-2">{{ $category->category_name }} {{ $category->cat_id }}</h4>
<div class="swiper-container">
<div class="swiper-wrapper"> <!-- ok -->
#php $z = 1; #endphp
#foreach($shop['product'] as $product)
<!-- display products -->
#endfoeeach
i want to show category wise products.but it shows all product
what should i try??
You can use groupby because Eloquent uses the query builder internally, so you can do
GroupBy("categoryWise")
You should have write query like below:
$shop['category'] = Category::->whereHas('Product', function($query) use($translate){$query->where('product_status','=',1)->where('product_drop_status','=','no')->where('language_code','=',$translate)->orderBy('product_id','desc')->with('ProductImages');})->get();

How to check foreach loop for a property. Laravel

I have been trying to figure out how to filter my foreach loop and only display an image if my post has one. So far I have been trying different functions and #ifs but to no avail.
Here is my controller code:
<div class="container">
<div class="row">
<div class="col-sm-6">
#foreach($posts as $post)
<div>
<h1>{{$post->title}}</h1>
<p>{{$post->body}}</p>
<img src="{{url('img', $post->image)}}">
</div>
<hr>
#endforeach
</div>
</div>
</div>
You can use a simple #if:
#if ($post->image)
<img src="{{ url('img/' . $post->image) }}">
#endif
Or #isset:
#isset ($post->image)
<img src="{{ url('img/' . $post->image) }}">
#endisset
https://laravel.com/docs/5.5/blade#control-structures
You can use a simple #if statement to see if the image property of the current $post is set. This could look a little something like this:
#foreach($posts as $post)
<div>
<h1>{{$post->title}}</h1>
<p>{{$post->body}}</p>
#if(!empty($post->image))
<img src="{{url('img', $post->image)}}">
#endif
</div>
<hr>
#endforeach
By including this, the <img> element will only be displayed if the property is not empty.
As also mentioned in another answer, you can replace the #if(!empty(...)) by #isset(...) to achieve the same result.

Insert a div in random intervals Laravel collection

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)

Laravel: blade foreach looping bootstrap columns

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>

How to skip first item in a foreach loop in Laravel?

I am trying to fill my webpage with content based on content stored in a database. However, I would like to skip the first item; I want to start looping from the second item.
How can I achieve this?
#foreach($aboutcontent as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach
As of Laravel 5.4, whenever you use foreach or for within blade files you will now have access to a $loop variable. The $loop variable provides many useful properties and methods, one of them being useful here, for skipping the first iteration. See the example below, which is a much cleaner way of achieving the same result as the other older answers here:
#foreach ($rows as $row)
#if ($loop->first) #continue #endif
{{ $row->name }}<br/>
#endforeach
Try This :
#foreach($aboutcontent as $key => $about)
#if($key > 0){
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endif;
#endforeach
Assuming that the $aboutcontents is numeric array just use the good old fashioned for loop instead of your new fangled foreach
// Notice you start at 1 and your first
// elem is 0 so... ta da... skipped
#for ($i = 1; $i < count($aboutcontents); $i++){
$about = $aboutcontents[$i]; //This is the object
//now use $about as you would
}
Note: I have not used Larvel or blades but based on the docs this should be doable
There is two way to do this:
1- if your $key is numeric you can use:
#foreach($aboutcontent as $key => $about)
#if($key == 0)
#continue
#endif
code
#endforeach
2- if a $key is not numeric
use #loop->first as a condition
You need some kind of a counter if you want to do that in blade:
<?php $count = 0;?>
#foreach
#if($count>1)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endif
$count++
#endforeach
EDIT:
I like the answer provided by Mark Baker in the comment better
#foreach(array_slice($aboutcontent, 1) as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach
Alternatively, you can just remove the first element from the array before iterating:
#php
array_shift($aboutcontent);
#endphp
#foreach($aboutcontent as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach
The advantage is that you do not need any conditions to verify you are not the in the first iteration. The disadvantage is that you might need the first element within the same view, but that we don't know from your example.
Note It might make more sense to remove the first element from the array before you pass on the data to the view.
For reference, see:
http://php.net/manual/en/function.array-shift.php
https://laravel.com/docs/5.4/blade#php
Try this
#foreach($aboutcontent->slice(1) as $about)
<div class="col-md-4 text-center">
<div class="thumbnail">
<img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
<div class="caption">
<h3>{{ $about->aboutname }}</h3>
<p>{{ $about->aboutinfo }}</p>
</div>
</div>
</div>
#endforeach

Categories