How to make a variable optional in blade template (laravel 8) - laravel

I get an error :
$count is undefined
Make the variable optional in the blade template. Replace {{ $count }} with {{ $count ?? '' }}
Here is my blade file:
#extends('layouts.layout')
#section('content')
<div class="container">
<br>
#if ($count == 0 && !$count == '')
<h3>Δε βρέθηκαν eshops με τον όρο αναζήτησης : <span class="badge badge-secondary">{{ $q }}</span>.
<br>
Θα κάνουμε ό,τι μπορούμε να καλύψουμε την ανάγκη σας !<h3>
<h5>Μπορείτε να δοκιμάσετε με ένα νέο όρο αναζήτησης ή επιβεβαιώστε την ορθογραφία σας.</h5>
<button type="button" class="btn btn-success">Δοκιμάστε πάλι !</button>
#else
<h3>Βρέθηκαν <span class="badge badge-secondary">{{ $count }}</span> eshops με τον όρο αναζήτησης : <span class="badge badge-secondary">{{ $q }}</span></h3>
#endif
#foreach($eshops as $eshop)
#include('includes.eshop')
<br>
#endforeach
</div>
#endsection

You could use the blade #isset or #empty as well:
#isset($count)
// $count is defined and is not null...
#endisset
#empty($count)
// $count is "empty"...
#endempty
More info can be found here: If Statements

Instead of {{ $count }} try this {{ isset($count) ? $count : ''}}

// Try this method.
{{( !empty($count)) ? $count : 'No Logs Found' }}

You can Try This.
#if(isset($count))
.....
#endif

I think shortest and most elegant would be:
{{ $variable ?? '' }}
or
{{ $variable ?? null }}
or
{{ $variable ?? 'Default value' }}

Related

I can't use variable from #props in Laravel blade

#props(['active' => false])
#php
$classes = 'block text-left px-3 text-sm leading-6 hover:bg-blue-500 focus:bg-blue-500` `hover:text-white focus:text-white';
if ($active) $classes .= ' bg-blue-500 text-white';
#endphp
<a {{ $attributes(['class'=> $classes]) }}>
{{ $slot }}
</a>
If statement doesn't access to $active variable, how can I fix it

Laravel | Bootstrap 5 pills - class active/show

how can I correct add show and active class to bootstrap 5 pills?? Now I try to do something like this:
<div class="nav-head">
<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
#foreach($blocks as $item)
#foreach ($item->offers->take(4) as $offer)
<li class="nav-item" role="presentation">
<a class="nav-link {{ $offer->first ? 'active' : ''}} {{ $offer->first ? 'show' : ''}}" data-bs-toggle="tab" href="#tabs-{{ $offer->id }}">{{ $offer->title }}</a>
</li>
#endforeach
#endforeach
</ul>
</div>
<div class="tab-content" id="pills-tabContent">
#foreach($blocks as $item)
#foreach ($item->offers as $offer)
<div class="tab-pane fade {{ $offer->first ? 'active' : ''}} {{ $offer->first ? 'show' : ''}}" id="tabs-{{ $offer->id }}" role="tabpanel" aria-labelledby="live-chat-tab">
...
</div>
#endforeach
#endforeach
</div>
Line:
{{ $offer->first ? 'active' : ''}} {{ $offer->first ? 'show' : ''}}
but it doesn't work. Can you help me? Thanks!
It should be $loop->first not $offer->first
When looping, a $loop variable will be available inside of your loop. This variable provides access to some useful bits of information such as the current loop index and whether this is the first or last iteration through the loop:
{{$loop->first ? 'active' : ''}} {{$loop->first ? 'show' : ''}}
Ref:https://laravel.com/docs/8.x/blade#the-loop-variable

Undefined variable: product_item in laravel

I want to update selected items and this is my code:
controller:
public function updatesyncFiles(Request $request , $id){
$product_item = Singleproduct::find($id);
$files = $request->input('files');
if ($product_item && is_array($files)){
$product_item->file()->sync($files);
}
}
blade:
#if( $files && count($files) > 0)
<form action="{{ route('product.sync_files' , $product_item ) }}" method="post">
{{ csrf_field() }}
<h3 style="color: black; ">فایل مربوطه:</h3>
<section class="panel">
<table class="table table-striped table-advance table-hover">
<ul>
#foreach($files as $file)
<li>
<input type="checkbox" name="files[]" value="{{ $file->file_id }}" {{ isset($id) && in_array($file->id,$id) ? 'checked':'' }}>
{{ $file->file_name }}
</li>
#endforeach
</ul>
<div class="form-group">
<input type="submit" class="btn btn-danger" name="submit_product_files" value="ذخیره اطلاعات">
</div>
</table>
</section>
</form>
#endif
and the error is: "Undefined variable: product_item"
what is the problem??
You should pass your variables to the view like this:
return view('your_view', [
'product_item' => $product_item
]);
Also not that you should pass an array to 2nd argument of the route method:
{{ route('product.sync_files', ['product_id' => $product_item]) }}

Accessing collection from a relationship

So I have 2 tables.
DirtyEvent model and Event model.
I am retrieving DirtyEvent with Event which works fine
In blade I have:
#if (\Request::is('profile/event'))
#foreach ($events as $event)
#if (empty( $event->image ))
<div class="card-header">
<span class="card-title"> {{($event->title) }}</span>
</div>
#else
<div class="card-image">
<img class="img-responsive" src="{{ $event->image }}"></img>
<span class="card-title"> {{($event->title) }}</span>
</div>
#endif
<div class="card-content">
<p><strong>Starts: </strong>#php echo ($startdate) #endphp - {{$event->startime }}</p>
<br>
#if ($startdate != $enddate)
<p><strong>Ends: </strong>#php echo ($enddate) #endphp - {{$event->endtime }}</p>
<br>
#endif
<p><strong>Description:</strong></p>
<br>
<p>{{$event->description }}</p>
</div>
<div class="card-action">
<i class="fa fa-pencil-square-o fa-2x" aria-hidden="true"></i>
<form method="POST" action={{ url('events/delete/' . $event->id) }}>
{{ method_field('PATCH') }}
{{ csrf_field() }}
<button type="submit" class="delete" style="border:none;"><i class="fa fa-trash-o fa-2x" aria-hidden="true"></i></button>
</form>
</div>
#foreach ($events->publicevents as $eventss)
{{dd($events)}}
#endforeach
#endforeach
However dd($events) gives me:
DirtyEvent Collection -> relations -> publicevents -> Event collection
But, It is saying that publicevents do not exist in current collection.
Controller
public function index()
{
$id = Auth::id();
$events = DirtyEvent::where('user_id', $id)
->with('publicevents')
->get();
return view('events.viewEvent', compact('events'));
}
Model
public function publicevents()
{
return $this->hasMany('App\Event', 'event_id');
}
I guess not all dirty event objects have events. So check this first with empty() or isEmpty() or count():
#if (!empty($event->publicevents))
#foreach ($event->publicevents as $eventss)
{{ $eventss->id }}
#endforeach
#endif
Update
It should be $event->publicevents, not $events->publicevetns.

How to remove comma from last array in Laravel 5?

I have this view :
<p style="text-align: center;"><strong>
#foreach($journal->user as $item)
{{ $item->name }},
#endforeach
</strong></p>
I wanted to remove comma after the last {{ $item->name }} string. How to do it in Laravel 5.3 blade ?
If you're using 5.3, you can use $loop variable for this:
#foreach($journal->user as $item)
{{ $loop->first ? '' : ', ' }}
{{ $item->name }}
#endforeach
The code is from similar question.
Try this
#foreach($journal->user as $item)
{{ $item->name }}
#if (!$loop->last)
,
#endif
#endforeach

Resources