Switching styles in CSS, under the specified conditions laravel - php

I need to specify my styles under certain conditions
<div class="flex-container">
#foreach($topServers as $server)
<div class="flex-item">
<div class="row">
#foreach($guilds as $guild)
#if($server->id == $guild->id)
<img class="col-md-auto avatar" src="..." alt="">
#endif
#endforeach
</div>
</div>
#endforeach
</div>
Provided that $server->premium >= 1, you need to specify the "border-color:gold" styles in the div class="flex-item"
And I absolutely do not understand how to implement it, thank you in advance for your help

Just use a ternary to add a style.
<div class="flex-item" {{ $server->premium >= 1 ? 'style="border-color:gold"' : '' }}>

Related

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.

bootstrap 4 grid row and php #foreach

I am trying to switch over a site to Bootstrap 4 from Bootstrap3 and having an issue with the Grid "row" option when nesting with looping through a PHP Laravel #foreach loop.
The code that I have tried in different ways looks like:
<div class="row">
<div class="col-12">
<div class="row">
#foreach($sofas as $sofa)
#if(isset($sofa['Sub']))
<a href="{{ Request::url() }}/{{ $sofa['Style'] }}">
<div class="col-12 col-sm-4 col-md-3" style="padding:0 5px 0 5px;">
<div class="product_tile">
<div class="product_tile_style">{{ $sofa['StyleName'] }}</div>
<div class="product_tile_frame">Frame {{ $sofa['Style'] }}</div>
<div class="product_tile_thumb">
<img width="200" height="140" src="/imagelib/bigthumbs/{{ $sofa['url'] }} " alt="">
</div>
</div>
</div>
</a>
#endif
#endforeach
</div>
</div>
The expected outcome should look like the following:
Example of what it should look like.
And this is the random mess that I am getting where it is shrinking everything and not expanding to the columns.
Example of what I am getting which is wrong.
Any ideas and help would be greatly appreciated.
Thank you all in advance!

Laravel 5.4 checking if chunk is first chunk

I'm trying to set a condition check whether the chunk is the first batch of chunk or not.
Anyone know how to check what the chunk id or something similar for example:
#foreach($item->chunk(3) as $item_chunk
#if(chunk is first chunk)
<div class="carousel-item active">
#else
<div class="carousel-item">
#endif
#foreach($item_chunk as $item)
{{-- Do something --}}
#endforeach
#endforeach
Appreciate it if someone knows of a way to check whether the chunked list is first or not.
You can check loop using $loop variable.
#foreach($item->chunk(3) as $item_chunk)
#if($loop->first)
<div class="carousel-item active">
#else
<div class="carousel-item">
#endif
#foreach($item_chunk as $item)
{{-- Do something --}}
#endforeach
#endforeach
For sliders
<div class="carousel-inner">
#foreach($item_hr as $count => $valuetopproducts)
<div class="item {!! $count == 0 ? 'active' : '' !!}">
<img src="{{Storage::url('wp/thumbnail')}}/{{$valuetopproducts->imagename}}" class="img-responsive" alt="{!! \Illuminate\Support\Str::words($valuetopproducts->assetname, 10,'...') !!}">
</div>
#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

How to cut the content in laravel5?

I need to cut the characters that show description with the same quantity of letter in all foreach.
Could anyone helps me?
#foreach ($subastas as $subasta)
#if(($subasta->data_inici <= $data)&&($subasta->data_final>$data))
<a href="item/{{$subasta->id}}"><div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<div class="nombre">{{$subasta->nombre}}</div>
#foreach ($subasta->images as $word => $meaning)
#if($word==0)
<img src="{{ asset($meaning->ruta) }}" style="width="242px" height="164px""/>
#endif
#endforeach
<h2><span class="clock" data-countdown="{{ $subasta->data_final}}"></span></h2>
<p>{{$subasta->descripcion}}</p>
<p>{{$subasta->cant_actual}}</p>
<a class="pujar btn btn-warning" id="{{$subasta->id}}">Pujar</a>
</div>
</div>
</div>
</div></a>
#endif
#endforeach
I think and image is better than explain .
Do you mean you want to limit the number of characters in a string?
This can be done in laravel by a string helper:
$value = str_limit('The PHP framework for web artisans.', 7);
So in your example:
<p>{{ str_limit($subasta->descripcion, 20) }}</p>
When whoever inputs the description of the product, can you not use max-length within the HTML element to set the limit prior to being saved?
<input type="text" maxlength="250" />

Categories