Filtering array in Blade - php

I need to show a certain picture. If a picture has is_primary property filled and true then I need to show that picture. Otherwise, I should just display whatever is first. Here is my code. It doesn't work, I see both is_primary picture and first. What am I doing wrong?
#php
$check = 0
#endphp
#foreach ($ad->photos as $photo)
#if ($photo->is_primary == true)
<img src="{{$photo->filename}}" alt="{{$ad->name}}" class="card-image">
#php
$check = 1
#endphp
#endif
#endforeach
#if ($check > 0)
<img src="{{$ad->photos->first()->filename}}" alt="{{$ad->name}}" class="card-image">
#endif

You have issue in you logic. If is_primary is true you are displaying image and set flag variable to 1. And end of loop you are checking $check > 0 so its true when $check =1 you need to add condition $check == 0
#php
$check = 0
#endphp
#foreach ($ad->photos as $photo)
#if ($photo->is_primary == true)
<img src="{{$photo->filename}}" alt="{{$ad->name}}" class="card-image">
#php
$check = 1
#endphp
#endif
#endforeach
#if ($check == 0)
<img src="{{$ad->photos->first()->filename}}" alt="{{$ad->name}}" class="card-image">
#endif
OR you can check != 1
#if ($check != 1)
<img src="{{$ad->photos->first()->filename}}" alt="{{$ad->name}}" class="card-image">
#endif

Related

#php tag in blade template can't reach inline variable

I am looping through products which has all my configurable options available. Inside that i am looping through my items, when i get a match in product i want to display this product and item.
To do so i want to create a variable called $currentProduct.
#foreach($order['products'] as $product) // Configurable products
#foreach($order['items'] as $item) // Simple products
#if ($item['product_type'] == 'simple')
#if ($product['id'] === $item['product_id'])
#php($currentProduct = $product) // error here $product undefined
#php($currentItem = $item) // error here $item undefined
#endif
#elseif ($item['product_type'] == 'configurable')
#if ($product['id'] == $item['parent_item']['product_id'])
#php($currentProduct = $product) // error here $product undefined
#php($currentItem = $item) // error here $item undefined
#endif
#endif
#endforeach
#endforeach
I am new to laravel and blade templates and can't seem to wrap my head around, why $product and $item is undefined when they are defined right above in same template.
Any help appreciated
You put the code between #php and #endphp but without parantheses.
You may have success using the $loop variable:
#foreach($order['products'] as $product) // Configurable products
#foreach($order['items'] as $item) // Simple products
#if ($item['product_type'] == 'simple')
#if ($product['id'] === $item['product_id'])
#php
$currentProduct = $order['products'][$loop->parent->index];
$currentItem = $order['items'][$loop->index];
#endphp
#endif
#elseif ($item['product_type'] == 'configurable')
#if ($product['id'] == $item['parent_item']['product_id'])
#php
$currentProduct = $order['products'][$loop->parent->index];
$currentItem = $order['items'][$loop->index];
#endphp
#endif
#endif
#endforeach
#endforeach
The variables can then be used below where they were defined.
See documentation:
$loop properties: https://laravel.com/docs/master/blade#the-loop-variable
Raw PHP in blade templates: https://laravel.com/docs/8.x/blade#raw-php
any #php must close with #endphp
Would you please try the below code?
`
#foreach($order['products'] as $product)
#foreach($order['items'] as $item)
#if ($item['product_type'] == 'simple')
#if ($product['id'] === $item['product_id'])
#php ($currentProduct = $product) #endphp
#php ($currentItem = $item) #endphp
#endif
#elseif ($item['product_type'] == 'configurable')
#if ($product['id'] == $item['parent_item']['product_id'])
#php ($currentProduct = $product) #endphp
#php ($currentItem = $item) #endphp
#endif
#endif
#endforeach
#endforeach
`

How to check session value at Blade

I'm working with Laravel 5.8 and I want to set a session variable at cart.blade.php Blade, like this:
#php
$conflicted = '';
#endphp
#if($conflicting && ($prd[0]->prd_delivery == 'city_free_delivery' || $prd[0]->prd_delivery == 'country_free_delivery'))
#php Session::put($conflicted , '0') #endphp
<p style="color:red;">
This product has free delivery but it can not be set because of service area conflict of other products
</p>
#endif
#if(!$conflicting && ($prd[0]->prd_delivery == 'country_free_delivery'))
#php Session::put($conflicted , '1') #endphp
<p style="color:red;">
Country Free Delivery
</p>
#endif
#if(!$conflicting && ($prd[0]->prd_delivery == 'city_free_delivery'))
#php Session::put($conflicted , '2') #endphp
<p style="color:red;">
City Free Delivery
</p>
#endif
Now on checkout.blade.php, I need to check the session value of $conflicted variable.
So how can I check session value at Blade ?
Because with get and has method, I can check the session key name.
You can use session() helper:
#if (session('conflicted'))
{{ session('conflicted') }}
#endif

Refactor to show link or not in blade

I felt this code look a bit messy, the logic is to show a link <a href or otherwise show text only.
How can I refactor this to look cleaner and maintainable?
<ol class="breadcrumb">
<li class="{{ $active == 'sign_in'? 'active':'' }}">
#if($active != 'sign_in')
#php($showLink = true)
#else
#php($showLink = false)
#endif
#if($showLink)
<a href="{{ url_secure('sign_in') }}">
#endif
Sign In
#if($showLink)
</a>
#endif
</li>
<li class="{{ $active == 'article'? 'active':'' }}">
#if($active != 'article' && $showLink)
#php($showLink= true)
#else
#php($showLink= false)
#endif
#if($showLink)
<a href="{{ url_secure('article')}}">
#endif
Articles
#if($showLink)</a>#endif
</li>
<li> </li> //repeat the code logic like above
</ol>
It would be good if there a way to reduce if conditions and use loop.
Why not just:
<li class="{{ $active == 'sign_in'? 'active':'' }}">
#if ($active != 'sign_in')
Sign In
#else
Sign In
#endif
</li>
Also it is unclear why you check $showLink that was set previoulsy.
Okay, as I've read your comments, I've modified template a bit with using foreach loop:
#php
// some initial variables
$allowed_to_click = true;
// this is a link to selected page
$selected_link = 'something';
#endphp
#foreach ($links as $link)
#php
// check if this link is ACTIVE
$active = $link == $selected_link;
// check if we can CLICK this link
$allowed_to_click = $active || $allowed_to_click;
#endphp
<li class="{{ $active ? 'active':'' }}">
#if ($allowed_to_click)
Some Caption
#else
Some Caption
#endif
</li>
#php
// if link is ACTIVE - following links are unclickable
if ($active) {
$allowed_to_click = false;
}
#endphp
#endforeach

Laravel 5 after 3 posts show an add

Hello guys i have declared my posts with variable $lastItems
#if($lastItems->total() > 0)
#foreach($lastItems as $item)
#include('._posting.items')
#endforeach
#else
#include('errors.emptycontent')
#endif
and i want to add ads after each 3 post, i use something like this
#if($lastItems->total() > 0)
#foreach($lastItems as $item)
#include('._posting.items')
#endforeach
#if($item = 3)
<img src="link" alt="ads">
#endif
#else
#include('errors.emptycontent')
#endif
but the problem is, when i try like this it shows the ads after every post not after 3 posts.
What i'm doing wrong?
You may try something like:
#if($lastItems->total() > 0)
#foreach($lastItems as $key => $item)
#include('._posting.items')
#if (($key + 1) % 3 == 0)
<img src="link" alt="ads">
#endif
#endforeach
#else
#include('errors.emptycontent')
#endif
I had almost same problem, in my case I had to change div's class. So I used sth like this and it helped me:
<?php $var = -1; ?>
#foreach($lastItems as $item)
<?php $var ++;?>
#if($var%3 == 0 && $var!=0)
#include('errors.emptycontent')
else
#include('._posting.items')
#endif
#endforeach
I hope it helps :)

Set Background Color of a <td> in a #if in Blade Templating

I have a Table cell like this:
<td>
#if ($user->status == 1)Present
#elseif ($user->status_detail != null) Absent(see Details)
#else Absent
#endif
</td>
So my problem is, I don't want to just give the Table cell the Word "Present" I want to give it also a Color. Do you guys have a suggestion for this problem?
You need to add this in element as in the following example
<td class="
#if ($user->status == 1)
present
#else
absent
#endif
">
#if ($user->status == 1)Present
#elseif ($user->status_detail != null) Absent(see Details)
#else Absent
#endif
</td>
And then in CSS you can define styles as you want for example:
.present {background:#0f0;}
.absent {background:#f00;}
Give a tag around the text "Present". then give a class to the span use CSS to give background color to span.
<td>
#if ($user->status == 1)<span class="present">Present</span>
#elseif ($user->status_detail != null) Absent(see Details)
#else <span class="absent">Absent</span>
#endif
</td>
In CSS
.present{ background:#ff0;display:block; width:100%;}
.absent{ background:#f00;display:block; width:100%;}
#if ($user->status == 1)
$status = Present;
$color = "green";
#elseif ($user->status_detail != null)
$status = Absent(see Details);
$color = "red"
#else
$status = Absent(see Details);
$color = "red";
#endif
<td style="background-color:{{$color}}">{{$status}}</td>
<td class="status">
#if ($user->status == 1)Present
#elseif ($user->status_detail != null) Absent(see Details)
#else Absent
#endif
</td>
CSS
.status{ background:#fff;}
or should it be different when absent or present?

Categories