Blade #if / #else returns both statements - php

I have an #if/#else condition in a Blade file to present different values of a PHP table. The data comes from a Laravel/Livewire controller, and the Blade code that creates the issue is below.
#if(!$course->user_limit)
#if(in_array($course->id, $arrs))
<x-jet-button wire:click="confirmCourseInterest( {{ $course->id}})"
class="bg-orange-500 hover:bg-orange-700">
{{__('Κάντε κλικ για απόσυρση ενδιαφέροντος')}}
</x-jet-button>
#else
<x-jet-button wire:click="confirmCourseInterest( {{ $course->id}})"
class="bg-orange-500 hover:bg-orange-700">
{{__('Κάντε κλικ για εκδήλωση ενδιαφέροντος')}}
</x-jet-button>
#endif
#else
<div class="flex" id="user_limit_reached">
{{ __('User Limit Reached') }}
</div>
#endif
The issue occurs on the outer if/else that irregularly returns both if and else statements. The user_limit is false on the field that has the problem. Why does this happen?

Change your #else to #elseif and pass another logic there.

Related

how to check is there a value in the other column of table?

In a laravel blade in third line of this code I want to check if parent_id exists in id column or not
please help me!
I'm using laravel 9
#if ($category->parent_id == 0)
no parent
#if ($category->parent_id)
no parent
#else
{{ $category->parent->name }}
#endif
I corrected it this way:
#elseif (empty($category->parent))
Using exists() function for parent()
Not that exists function works just with single relations (belongsTo, hasOne)
// This will run SQL query // returns boolean
$category->parent()->exists(); // Don't forget parentheses for parent()
If you want to save performance and not calling sql query
count($category->parent); // returns 0 if not exist
Balde:
you can use the empty() to check if empty.
#if ($category->parent == 0)
no parent
#elseif (empty($category->parent))
<p>no parent</p>
#else
{{ $category->parent->name }}
#endif
or ?? operator
{{ $category->parent_id ?? 'no parent' }}
You can use the is empty in twig as below:
{% if category is empty %}
<p> No parent </p>
{% endif %}
You can try by using isset() function
#if ($category->parent_id == 0)
no parent
#if (!isset($category->parent_id))
no parent
#else
{{ $category->parent->name }}
#endif

Display a section according Condition in laravel

I have two return varibles from controller to view in Laravel.
return view ('home', compact('books','count'));
Now I want to display one section('content') #if the conditon is true
in my home.blade.php. How I can do this. Now section('content') will display #if condition is false
#if (!empty($books))
#section('content')
#endif
You can use #else, #elseif, #unless
Here is an example
#if (count($books))
section('content')
#else
#section('your other section')
#endif
Hope this helps

#empty giving "Undefined variable: __empty_0"

I am currently trying to set up my blade layout with laravel 5.4 but I seem to be having issues with the #empty statement while using it with loops
The code I am trying to use is:
#foreach ($notifications as $notification)
<li data-for='{{ $notification->lobbyid }}'>
#if ($notification->approved == 1)
<div class='notification-text'><strong>{{ $notification->sentBy }}</strong> wants access to your lobby: \"{{$notification->title}}\".</div>
<div class='notification-actions' id='HI' data-for='{{ $notification->lobbyid }}' data-userid='{{ $notification->userid }}'><span data-type='{ \"as\": \"requesting\", \"action\": 1 }'>Accept</span><span data-type='{ \"as\": \"invited\", \"action\": 0 }'>Deny</span></div>
#else
<div class='notification-text'><strong>{{ $notification->sentBy }}</strong> invites you to join his lobby: \"{{$notification->title}}\".</div>
<div class='notification-actions' id='HI' data-for='{{ $notification->lobbyid }}' data-userid='{{ $notification->userid }}'><span data-type='{ \"as\": \"invited\", \"action\": 1 }'>Accept</span><span data-type='{ \"as\": \"invited\", \"action\": 0 }'>Deny</span></div>
#endif
</li>
#empty
<li>No Notifications</li>
#endforelse
And I get the following error:
Undefined variable: __empty_0
When used separately it works great:
#endforeach
#empty($notifications)
<li>No Notifications</li>
#endempty
But I would like the cleaner syntax like on the laravel documentation.
#foreach ($notifications as $notification)
it should be #forelse not #foreach

Error while fetching data from database in laravel 5.1

I am facing a problem while fetching the data from database
view.php
<p>attend</p>
#foreach($attendings as $attending)
#if ($attending->acceptance==1)
{{ $attending->membername }}
#endif
#endforeach
<p>not attend</p>
#foreach($attendings as $attending)
#if ($attending->acceptance==0)
{{ $attending->membername }}
#endif
#endforeach
<br>
<br>
<p>final decision </p>
{{ $attendings->editor_com }}
controller
public function attendx()
{
$attendings = DB::table('attendance')->get();
return view('My_Work.report', ['attendings' => $attendings]);
}
routes
get('/editor/report','DatabaseController#attendx');
But what I'm facing is Undefined variable: attendings
What am I doing wrong?
Note: I am writing the codes in laravel 5.1
You are returning attendings to this view file My_Work.report.blade.php
So you have to place all your code in report.blade.php file .
Remove this line
{{ $attendings->editor_com }}
because this will throw error Trying to get property of non-object

How to tackle variables that may not be set when using blade template engine to build forms

Ok, so a long title, but apologies, he only way to describe it without getting the wrong kind of answer.
So, the scenario....
I am creating a site which has a search form of sorts in the header, and therefore on every page. I would like it to retain its previous variables when being used for user convenience, for my convenience I have built the form into the default layout, to save recreating many instances of it.
default.blade.php (Heres the form, with unnecessary markup removed)
{{ Form::open(array('url' => '/search')) }}
{{ Form::select('model', Photo::getAvailableModels(true), $model) }}
{{ Form::select('colour', Photo::getAvailableColours(true), $colour) }}
{{ Form::submit('Go') }}
{{ Form::close() }}
The $model & $colour are variables I am capturing during the post. The problem is, I am getting unset variable errors from Blade on any pages where the user hasn't posted to, so I am literally having to preset them in almost every route or controller across my entire site, just to prevent the errors.
In essence, the system works fine as long as the user is posting, if someone visits the site using a direct link its basically useless.
Obviously I can not have the search form be set to the previously searched results, but that would be bad practice from a usability point of view.
Am I missing something here, surely there has to be a simple solution to this. Thanks for any help.
Create a view composer for your highly used variables:
View::composer(['store.index', 'products.*'], function($view)
{
$model = Input::get('model') ?: 'modelX';
$colour = Input::get('colour') ?: 'colourY';
$view->with('model', $model);
$view->with('colour', $colour);
});
Laravel will send those variables to your views automatically, every time someone hit one of them.
You can put that in your routes file, filters file or, like, me, create a app/composers.php and load by adding
require app_path().'/composers.php';
To your app/start/global.php.
You can check whether variables are set or not with PHP isset():
{{ Form::open(array('url' => '/search')) }}
#if (isset($model) && isset($colour))
{{ Form::select('model', Photo::getAvailableModels(true), $model) }}
{{ Form::select('colour', Photo::getAvailableColours(true), $colour) }}
#else
{{ Form::select('model', Photo::getAvailableModels(true)) }}
{{ Form::select('colour', Photo::getAvailableColours(true)) }}
#endif
{{ Form::submit('Go') }}
{{ Form::close() }}
Or, if your form fields are optional and you need to check separately:
{{ Form::open(array('url' => '/search')) }}
#if (isset($model))
{{ Form::select('model', Photo::getAvailableModels(true), $model) }}
#else
{{ Form::select('model', Photo::getAvailableModels(true)) }}
#endif
#if (isset($colour))
{{ Form::select('colour', Photo::getAvailableColours(true), $colour) }}
#else
{{ Form::select('colour', Photo::getAvailableColours(true)) }}
#endif
{{ Form::submit('Go') }}
{{ Form::close() }}

Categories