Laravel 4 Carbon Format with if statement - laravel

Does anyone know how I could get this to work.
My database has some null dates, so I would like to return as empty space.
I currently have this.
{{ Carbon::parse($chauffeur->roadTestCert)->format('m/d/Y') }}
and from what I read about the blade template is that you can use "or 'message'" after it.
I did this.
{{ Carbon::parse($chauffeur->roadTestCert)->format('m/d/Y') or '' }}
in hopes to just show empty space but I get a "1" instead.
Anyone know why and/or how to get around this?

Well, you should rather do it using simple condition:
#if ($chauffeur->roadTestCert !== null)
{{ Carbon::parse($chauffeur->roadTestCert)->format('m/d/Y') }}
#endif

Related

Laravel - Attempt to read property "id" on null

Hope you can help me. I'm trying a simple loop in a Laravel blade view and trying to access some data through a relationship.
The DD below outputs 1 as expected, but the echo in the h5 produces the error in the title. I'm stumped!
#forelse($user_adverts as $user_advert)
{{ dd($user_advert->advertLogs->first()->id) }}
<h5 class="card-title">£ {{ $user_advert->advertLogs->first()->id }}</h5>
#empty
<p>No adverts yet :(</p>
#endforelse
If I {{ dd($user_advert->advertLogs->first()) }} I see the model: App\Models\AdvertLog
I'm just confused why I can't output a single column without a null error? Usually that means there is no relationship I thought? I know this is going to be a simple one - your help is appreciated!
You can get rid of the error by changing the card title like this
{{ $user_advert->advertLogs->first()?->id }}
The id will be displayed if you have one, and nothing if there is no advertLogs.
Not sure if this is want you intented to do though.

Break loop and catch remaining items count

I'm trying to develop a feature.
There are users who like a post, however, after naming the only three users, I'd like to capture the rest into a number like so:
Jeff, John, Jane and 45 others liked this post.
This is what I have so far:
#foreach($post->liked as $liked){{ $loop->first ? '' : ', ' }}{{ $liked->user->name }}#if($loop->iteration == 1) {{ $liked->count() }} #break #endif #endforeach
Can anyone please shed light on how this can be done? Sorry, I'll be trying to figure it out in the meantime but just thought I'd ask in case I get stuck on it for too long.
Thanks in advance.
Edit:: I added $liked->count() which shows the number of likes, but how can I remove the number of the users that have already liked the post?
For the remaining item counts use $loop->remaining, it's inbuild feature of laravel.
You can check more about it here.
#foreach($post->liked as $liked)
{{ $loop->first ? '' : ', ' }}
{{ $liked->user->name }}
#if($loop->iteration == 1) and {{ $liked->count() - 1}} others #break
#endif
#endforeach
Use $liked->count() and subtract the number of users you already want displayed.

How do I write an #if in Laravel that uses data from my database?

I'm new to Laravel and am using Laravel 6. One of my views is going to contain the values in a row of my MySQL table. The table column is a boolean so it contains either 0 or 1. Rather than displaying 0 or 1 in the view, I want to display YES or NO. An #if seems the logical way to do this but I can't get my #if to work.
#if ({{ $sleepDiaryEntry->outdoorLight }} == 1 )
<p>Two hours of outdoor light? YES</p>
#else
<p>Two hours of outdoor light? NO</p>
#endif
I've tried several variations of the #if but every variation gives me a syntax error on the first line of the #if.
Unfortunately, the Laravel manual is very skimpy on details of exactly what arguments can and cannot appear in an #if. They tend to give an example or two and think they've anticipated every possible situation and question.
How I can accomplish what I want to do, either with or without #if?
remove {{}} in #if, like followings
#if ($sleepDiaryEntry->outdoorLight == 1 )
<p>Two hours of outdoor light? YES</p>
#else
<p>Two hours of outdoor light? NO</p>
#endif
because {{}} for show the variable
I am not sure about what you want but by using {{}} you are trying to print data.
Try :
#if ($sleepDiaryEntry->outdoorLight == 1 )
<p>Two hours of outdoor light? YES</p>
#else
<p>Two hours of outdoor light? NO</p>
#endif
If it doesn't work:
Are you giving $sleepDiaryEntry to the view in your controller method ?
Try to {{ dd($sleepDiaryEntry) }} see what you have in your view.
#if ({{ $sleepDiaryEntry->outdoorLight }} == 1 ) just gets rendered down to:
<?php if ({{ $sleepDiaryEntry->outdoorLight }} == 1 ) { ?>
in the final Blade template, which will cause a syntax error. You don't need the {{ }} tags in the conditional (not just "don't need": they won't be parsed at all here); it takes plain old PHP code in the ().

What does it mean {{ trans(core.text) }} in laravel?

I am new in Laravel and I am working on inbuilt project I have got {{ trans(core.text) }} after browse on internet I find that trans is a helper but I am not getting any file. So, Any one explain what does the line meaning {{ trans(core.text) }}.
Thank You
The trans function translates the given translation key using your localization files:
echo trans('messages.welcome');
If the specified translation key does not exist, the trans function will return the given key.
Link : https://laravel.com/docs/master/helpers

Ternary in Laravel Blade

Looking for a ternary operator for blade templates
#if(Auth::check()) ? yes : no #endif
Can't seem to get it to work this works
#if(Auth::check()) yes #else no #endif
suppose there is not much in it for this example, just curious.
You are free to use it with {{ }}.
{{ Auth::check() ? 'yes' : 'no' }}
This works:
{{ Auth::check() ? 'yes' : 'no' }}
I know this question was asked a while ago, but this may help somebody.
You can now do this in Laravel 5.
{{ $variable or "default" }}
Laravel 5 Blade Templates
Laravel 5.2 Blade Template
in addition, here is a nice shortcut ?:, if you you need to print some variable's value or if it's empty some default text
{{ $value ?: 'Default Value' }}
For Laravel 5 + php7, you should be using the null coalesce operator as explained in this Laravel News article, like so:
{{ $value ?? "Fallback" }}
Before the null coalescing operator, Blade handled the same problem with the “or” operator, which allows a default value when the first value isn’t set, separated by an “or”.
The ternary conditions look like this:
{{ condition ? 'yes' : 'no' }}
core php
if(!empty($mat->getSupplier)) {
echo $mat->getSupplier->supplier_name;
}
else if(!empty($mat->request_plant) {
echo \App\PlantName::whereId($mat->request_plant)->value('name');
}
else {
echo $mat->other_supplier;
}
laravel blade file
{{ (!empty($mat->getSupplier)) ? $mat->getSupplier->supplier_name : ((!empty($mat->request_plant)) ? \App\PlantName::whereId($mat->request_plant)->value('name'): $mat->other_supplier) }}

Resources