Laravel blade template once echoing variable and once not error - laravel

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

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.

Laravel blade syntax for #if/#endif spacing issueif

what is the similar blade syntax for the following code?
<?php if(...):?>abc<?php else:?>cde<?php endif;?>
the code
#if(...) abc #else .... is no good as it ads a space before and after the "abc" the code #if(...)abc#endif (no spacing before and after html string) generates error ...
Thanks
Solution
The correct solution for this problem would be following:
#if(1==1){{ '1' }}#endif
This happens often and makes problem with "space sensitive" parts of code like values in <option> tags.
Having the #if(1==1) 1 #endif would be compiled to following which shows empty spaces around the number:
<?php if(1==1): ?> 1 <?php endif; ?>
Solution code would be compiled to this code:
<?php if(1==1): ?><?php echo e('1'); ?><?php endif; ?>
Which pretty much explains why this won't make additional spaces.
Did a bit more research and it looks like there is no solution as there is no spaceless tag in blade. I found a solution from someone who wrapping his string in extra hml tags (so that is easy as he ads spaces before and after the tag and the string inside tag si spaceless) but in my case I just need a string no spaces before and after ... I will go the old fashion php way ...
Try this:
#if(...) {{ 'abc' }} #else
I've run into similar problems with spaces.
A workaround I use is this:
Put your if statement into php tags
#php
$myVar = 'abc';
if(...) $myVar = 'cde';
#endphp
and then echo the defined variable
{{$myVar}}
It's a workaround, but I still think we always should remember that it's a PHP environment...
So, simply:
#php
if(...) echo "abc";
else echo "cde";
#endphp
I have found the solution in case there is space issue between #if()#endif and #if.
I have replaced the #if() #endif with the {{$wardname}} variable to be printed using {{$wardname}}#if and its removed conflict with #endif & #if
and applied logic like:
if($city->wardname!="") {
$wardname = "(".$city->wardname.")";
}else{
$wardname = "";
}
Implemented it as:
{{$wardname}}#if
The correct syntax is:
#if(...)
abc
#else
cde
#endif
I never saw unwanted spaces with this one.

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 )

if the array were empty in laravel . then how to dispaly the message that their are no record found

#foreach($array as $value)
{{$value}}
#endforeach
$array is my array pass from Laravel controller then i want to display the message record not found if the array were empty.
this is my code example.
Besides 'foreach' loops, we also got 'forelse' loops in laravel blade template, what exactly this 'forelse' loops does? and most importantly should we care about it?
The 'forelse' loops is the better version of 'foreach', so yes, you should care, 'forelse' loops works exactly as 'foreach' except it also check the value is empty or not.
So with 'foreach' normally you check first whether the value is empty or not using 'if' statement, using 'forelse' you don't need to do that, the value is automatically checked.
#forelse ($array as $value)
{{ $value }}
#empty
There are no record found.
#endforelse
You can use #forelseas Shoukat Mirza in his answer and you also could use #if clause:
#if (count($array) > 0)
// foreach loop here
#endif
This is also helpful when you have multiple conditionals to check.

how to define a variable in laravel blade

How can define a variable in the view side(blade) in laravel?
I found that I can do it in this way:
<?php $var = 'something' ?>
But is there any way to do this like {{ $var = 'something' }} or #var1 = 'something' ?(ofcourse without printing it)
I agree with #Kiril Ivanov answer, but if you still want to do that you can use
#php ($variable = 'test')
Thanks
no, there is no way to define a variable with blade syntax except using the php syntax you have pointed. actually it is not a good practice to define variables in your views and do complex stuff except loops and conditional statements
yes there is a way to do this
first assign your variable like this
{{ $yourvariable='' }}
and after than u can manipulate the variable
#if ($abc['type']=='youresult')
{{ $yourvariable='success'}}
#endif
Hpe this works

Resources