Which of those two is the best practice using blade in laravel:
<p>{{ $person->name }} {{ $person->surname }}</p> or <p>{{ $person->name . ' ' . $person->surname }}</p>
Thanks!
Neither is a good option because in case one is empty (first or last name) - you will have an extra space that doesn't mean anything.
Best practises are:
1) Implement a method like getName() on your Person class that does the concatenation for you. Eg.
public function getName()
{
return implode(' ', array_filter([$this->name, $this->surname])));
}
And in your template it would become just <p>{{ $person->getName() }}</p>
2) If you have more attributes that require alteration before rendering - a common practise is to extract them all to a presenter class (that's a design pattern). Then your template would look like <p>{{ $person->presenter()->getName() }}</p>. But if it's just the name - you are probably fine just keeping it in the Person class.
It's usually all down to personal preference, Both are perfectly valid ways to do it, I like keeping everything seperate because at least for me (not everyone might think this is easier/cleaner), it becomes so much easier to add extra markup like this;
<b>{{ $person->surname }}</b>, <small class='text-muted'>{{ $person->name }}</small>
I always like to think of it in terms of dumb days, if you're having one of those days where you just can't think, which is easier to read for you :)
Related
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.
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.
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 ().
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
In the project I'm working on, I need to display 5 of the latest news articles on the website. In the Controller, I have written the following code:
$news = $repository->createQueryBuilder('p')
->Where('p.contenttype = :type')
->setParameter('type', 'newsarticle')
->orderBy('p.lastedit', 'ASC')
->getQuery();
$latestnews = $news->getResult();
This doesn't work for some reason, as I get the error message:
Item "url" for "Array" does not exist in "ShoutMainBundle:Default:page.html.twig" at line 34
However, when I change the getResult(); to getSingleResult(); it works, but will only display the one record (which is what I expect when I use that code).
This is where I come unstuck and confused about what I'm supposed to be doing. I have googled "how to display multiple records in symfony" and I haven't found the answer. (If the answer has been out there, I apologise in advance for this). In normal PHP, I would expect to do a foreach loop (something similar anyway) to get the results which I need. But I also have a feeling that to achieve what I want I need to do something in Twig. But what I need to do I don't know.
Any help with this would be very much appreciated.
Thanks
Edit:
Here is the template code that is used to display this:
<section id="latestnews">
<h2>Latest News</h2>
<ul>
<li><a href="..{{ news.url }}" title="Read {{ news.title }}" />{{ news.title }}</a></li>
</ul>
</section>
Your code tries to read from the variable news, and assumes that this variable has fields url and title. If your controller returns an array, you have to tread news as an array and iterate over it.
<section id="latestnews">
<h2>Latest News</h2>
<ul>
{% for news in latestnews %}
<li><a href="..{{ news.url }}" title="Read {{ news.title }}" />{{ news.title }}</a></li>
{% endfor %}
</ul>
</section>
It looks like in your template, you're looking for an object that's not found. It's looking for the url in an array object, but it doesn't exist. I think you need to put in a check, to see if that exists in the array, and then echo if it does. So something like if(news.url) echo news.url;
It may not be that exact syntax obviously, I'm not all that familiar with twig, but something similar to that.
You need to loop through the "news" results array in Twig.
{% for n in news %}
<li><a href="..{{ n.url }}" title="Read {{ n.title }}" />{{ n.title }}</a></li>
{% endfor %}