I don’t understand how #if statements work in Laravel Blade - laravel

I started learn Laravel and ran into an #if statements problem. How must I write a condition for empty places of code so that it is correct?
#if( !Request::is('coming-soon') )
#elseif( !Request::is('error') )
#else
#include('layouts.includes.header.main-menu')
#endif
I don't need this view in 'coming-soon' and 'error' pages.

You are doing it opposite:
I hope this is what you are looking for
#if( !Request::is('coming-soon') && !Request::is('error') )
#include('layouts.includes.header.main-menu')
#endif
This will include the page if the requested url is not "coming-soon" or "error".

Related

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 ().

Laravel blade template once echoing variable and once not error

I recently found strange behavior of blade in Laravel 5.8.
Let's say I have in file
#extends('layout')
#section('title','Dodaj playlistę')
#section('content')
{{$hosts = \App\Host::all()}}
<h2>Dodaj audycję do bazy</h2>
in the middle of the file I have
{{$hosts = \App\Host::all()}}
#foreach ($hosts as $man)
<option value='{{$man->id}}'>{{$man->name}} </option>
#endforeach
The problem is I always get the first $hosts variable echoed, while second not. What the hell? Such variable shouldn't be echoed at all because the command is only variable value attribution.
I've checked all my routes, there is no dd() or var_dump() command anyhere.
Looks like possibly a typo pushing for two sections within one. It should normally throw an error, but might be getting confused with the different language.
Change #section('title','Dodaj playlistę') to #section('title') and give it a try.
Also, set the var directly in the foreach for better clarity:
#foreach ($hosts as $man)
Becomes
#foreach(\App\Host::all() as $man)
{{$hosts = \App\Host::all()}} that will echo "\App\Host::all()"
you can
#php
$hosts = \App\Host::all()
#endphp
but it is discouraged to do in a view
see here How to Set Variables in a Laravel Blade Template

how to pass dynamic value in #include as blade name in laravel5.4

This is my code in laravel blade
#foreach($modules as $module) // {{ mod.$module['module_name'] }}
giving me right value
#section('content')
#include("mod.$module['module_name']") // mod is my folder name and
$module['module_name'] value is
coming from database
#endsection
#endforeach
{{ mod.$module['module_name'] }} is giving me correct value but when i pass it to #include it gives me error. Please help me !!!
I think it has to do with the way the blade template translates the #include directive into compiled PHP. Including the snippet from the compiled PHP file would help, but try this:
#include( 'mod.' . $module['module_name'] )
Looking at some of my past code, you can definitely use a variable in an include. In my case I simply built the desired filename in the controller and passed it along to the view, so my include statement was simply:
#include( $filename )

Laravel 4 Carbon Format with if statement

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

Blade template not executing #else clause

I have a Blade template that loads a list of users and displays their various details.
If a user has no mobile number I want to display the message "No Mobile Number" (I've tried single and double quotes), this never gets displayed:
#if ($person->Mobile >= "")
{{ $person->Mobile }}
#else
'No Mobile Number'
#endif
I tried substituting the "No Mobile" message with {{ $person->EMail }} (which I'm displaying elsewhere, so I know everyone has an email address), but still go nothing, as far as I can tell the logic isn't going into the #else block.
Any ideas?
This should work
#if (!empty($person->Mobile))
{{{ $person->Mobile }}}
#else
'No Mobile Number'
#endif
I use this approach:
#if( isset($error_message) )
#section('content')
#parent
#stop
#section('error_message')
<strong>Holly molly! </strong><em>{{ $error_message }} :(</em>
#stop
#endif
I have a section content and inside content have another section error_message so if error_message variable is set, show that content section and inside print my error_message.
PD: I haven't my original code to hand... Im currently using Twig as primary template engine in Laravel4. Twig beats Blade in simplycity and is very usefull

Resources