How to check session value at Blade - php

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

Related

Laravel blade on VSCode indention problem

Every time I do a format shortcut (Alt + Shift + F) for my .blade documents, codes that start with # do not indent the way I want them to be. Below is the sample code of an unformatted basic if/else + foreach on a .blade file.
// unformatted codeblock
<h1 class="poppins-medium text-xl text-darkergreen text-center">
#if (Auth::user()->role == 'auditor' || Auth::user()->role == 'accreditor')
#foreach ($documentGalleryTitle as $record)
#if ((Str::lower(Str::snake(Str::replaceLast(' Taskforce', '', $record->name)))) == $folder_name)
{{ Str::replaceLast(' Taskforce', '', $record->name) . ' Documents'}}
#endif
#endforeach
#elseif (Auth::user()->role == 'taskforce')
{{ $userRecord->nonOfficialsCategories->area_number . Auth::user()->subcategory . " Documents" }}
#endif
</h1>
As you can see on the previous codeblock, codes inside the parent #if/else should be indented. Same with the codes inside the #foreach, and the #if/endif codeblock.
I want the codeblock to look like the code below rather than the previous code above:
// how it should be
<h1 class="poppins-medium text-xl text-darkergreen text-center">
#if (Auth::user()->role == 'auditor' || Auth::user()->role == 'accreditor')
#foreach ($documentGalleryTitle as $record)
#if ((Str::lower(Str::snake(Str::replaceLast(' Taskforce', '', $record->name)))) == $folder_name)
{{ Str::replaceLast(' Taskforce', '', $record->name) . ' Documents'}}
#endif
#endforeach
#elseif (Auth::user()->role == 'taskforce')
{{ $userRecord->nonOfficialsCategories->area_number . Auth::user()->subcategory . " Documents" }}
#endif
</h1>
Is there a particular extension that I need to install + edit to create my desired format?

How to get number and sum of values of specific id (average) inside blade?

Template:
<div class="item-wrapper" style="text-align:justify; top: 100px;">
<legend>Reviews</legend>
#if (count($reviews)>0)
#forelse($reviews as $review)
#if( $review->counselor_id == $cprofile->id)
{{$review->review}}
{{$review->rating}}
<star-rating :rating="{{$review->rating}}">
</star-rating>
#endif #empty
#endforelse
#endif
</div>
Controller:
$relations = [
'viewCprofile' => counselor::all(),
'reviews' => DB::table('counselor_reviews')
->select('*')
->get()
];
return view('viewCprofile', $relations);
Route:
Route::get('/viewCprofile', 'ProfilesController#getCprofiles');
Should I pass id to controller to get data?
you can use the following and complete your code
#if (count($reviews)>0)
#php $total_ratings = 0 #endphp
#forelse($reviews as $review)
#if( $review->counselor_id == $cprofile->id)
#php $total_ratings += $review->rating #endphp
{{$review->review}} {{$review->rating}}
<star-rating :rating="{{$review->rating}}"> </star-rating>
{{$total_ratings}}
#endif

Filtering array in Blade

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

Laravel 5.2 IF Statement Syntax, Change Name based on Value

So, I'm going to moving on from PHP native to Laravel framework.
See the code below, it's must be error, but this would tell you what I want. I'm confused to the IF statement in Laravel based my database.
<td>
{{
#if($book->status==1)
echo 'Available';
#elseif($book->status==2)
echo 'Lost';
#else
echo 'Not Available';
#endif
}}
</td>
Is that possible to print out the name based on database, example: if the value is 1, then I'll print out 'Lost' on my website? Thank you.
<td>
#if ($book->status == 1)
Available
#elseif ($book->status == 2)
Lost
#else
Not Available
#endif
</td>
A better place for this would be in the book model itself:
public function getAvailabilityAttribute()
{
if ($this->status == 1) return 'Available';
if ($this->status == 2) return 'Lost';
return 'Not Available';
}
Then you could use it directly in your view:
<td>{{ $book->availability }}</td>
Use like this:
<td>
#if($book->status==1)
{{ 'Available' }}
#elseif($book->status==2)
{{ 'Lost' }}
#else
{{ 'Not Available' }}
#endif
</td>

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