php - convert ternary operator to if else in laravel
Get the solution ↓↓↓I've created posts table and display on interface post table. so I want to display the owner of the post by using thier name. the first step is I use ternary operator but I want to change to if else statement. But If try second way will get the error:
syntax error, unexpected '{' (View:.
how the correct way If I want to use the second way.
First way
<td>{{ isset($post->user) ? $post->user->name : '' }}</td>
Second Way
<td>
@if(isset($post->user)
{
$post->user->name;
}
else
{
'';
});
@endif
</td>
Answer
Solution:
@if (...)
gets compiled to<?php if (...): ?>
. So you are no longer in the context of PHP tags between the Blade directives.
You can do this simply by using Blade though:
@if(isset($post->user))
{{ $post->user->name }}
@endif
--- which is basically:
<?php if (isset($post->user)): ?>
<?php echo e($post->user->name); ?>
<?php endif; ?>
There is no need for theelse
branch here with your example.
Alternative:
{{ $post->user->name ?? '' }}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: sqlstate[hy000] [1698] access denied for user 'root'@'localhost'
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.