Laravel if/else syntax - php

How do i set different states of the icon? I want it to change between a liked/not liked icon. Can i do that? I don't completely understand the syntax. The btn-success class is for the liked icon, and btn-danger is for the unliked button.
#if(Auth::check())
#if( !$unblock )
<span data-id="{{ $key->id }}" data-like="Like" data-like-active="Remove Like" title="#if(!empty($likeUser)) Remove Like #else Like #endif" class="btn btn-xs pull-left #if(!empty($likeUser)) btn-success #else btn-danger #endif btn-like likeButton #if(!empty($likeUser)) active #endif">
<i class="Like-icon"></i>
</span>
#else

You're almost right just complete the condition after #else
Here is the place to learn more about blades and templates
It is as simple like regular PHP if..else condition
#if (Auth::check())
Authorized
#else
Nope
#endif
If you want to have additional conditions inside then
#if (Auth::check())
Authorized
//your additional conditions for authorized users
#else
Nope
#endif

#if (count($records) === 1)
I have one record!
#elseif (count($records) > 1)
I have multiple records!
#else
I don't have any records!
#endif

Related

Display Selected Option (different href) in Dropdown Title

Im working on my Laravel project, and i wanna make some filter buttons for my data table.
Those buttons look like this : https://i.stack.imgur.com/Swpbw.png
I want to display the selected option in dropdown title. For example, the Author title should be changed to any others name like Admin or User etc... after selecting the option from the dropdown button.
Author Dropdown Button (original) :
<div class="dropdown">
<button class="btn btn-outline-dark btn-sm dropdown-toggle mr-2" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Author
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
#foreach($users as $user)
<a class="dropdown-item" href="/posts?user_id={{$user->id}}">
{{ $user->name }}
</a>
#endforeach
</div>
</div>
I found some solutions, they all look like this :
$(".dropdown-menu").on('click', 'a', function(){
$(".btn:first-child").text($(this).text());
$(".btn:first-child").val($(this).text());
});
But the problem is :
The solution above only works if the href (url) from dropdown-menu is fixed (href="#").
In my project, those dropdowns have different href, so the title can only be changed for a short moment after clicked, then back to default after the url changed.
I think we need to catch the url then make some conditions to display the correct title.
For example, my Category filter only have 3 fixed options so i can do it like this.
Category Dropdown Button :
#php
$request = Request::getRequestUri();
#endphp
<div class="dropdown">
<button class="btn btn-outline-dark btn-sm dropdown-toggle mr-2" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
#switch(true)
#case(strstr($request, 'category_id=1'))
Pc
#break
#case(strstr($request, 'category_id=2'))
Console
#break
#case(strstr($request, 'category_id=3'))
Handheld
#break
#default
Category
#endswitch
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="{{strstr($request, 'category_id=1') ? 'dropdown-item active' : 'dropdown-item'}}" href="/posts?category_id=1">
Pc
</a>
<a class="{{strstr($request, 'category_id=2') ? 'dropdown-item active' : 'dropdown-item'}}" href="/posts?category_id=2">
Console
</a>
<a class="{{strstr($request, 'category_id=3') ? 'dropdown-item active' : 'dropdown-item'}}" href="/posts?category_id=3">
Handheld
</a>
</div>
</div>
Meanwhile, i have a foreach loop in the Author filter, cause the number of users is not fixed. So i cannot use the same code like i did with Category.
I had tried this but it just doesn't work.
Author Dropdown Button (not working) :
<div class="dropdown">
<button class="btn btn-outline-dark btn-sm dropdown-toggle mr-2" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
#if($request == '/posts' OR strstr($request, 'posts?page='))
Author
#else
#foreach($users as $user)
#if(strstr($request, 'user_id={{$user->id}}'))
{{ $user->name }}
#endif
#endforeach
#endif
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
#foreach($users as $user)
<a class="dropdown-item" href="/posts?user_id={{$user->id}}">
{{ $user->name }}
</a>
#endforeach
</div>
</div>
It cannot display the user name on title when clicked (blank result).
I also tried this one, but not working either.
#foreach($users as $user)
#if($request == "/posts?user_id={{$user->id}}"))
{{ $user->name }}
#endif
#endforeach
Please help guys !
you still can use the JS solution by preventing the redirection of the link
$(".dropdown-menu").on('click', 'a', function(event){
event.preventDefault();
event.stopPropagation();
$(".btn:first-child").text($(this).text());
$(".btn:first-child").val($(this).text());
});
I've found the solution.
The if condition (inside the foreach) is right, but somehow it doesn't work in blade template.
So, just put it in the php section.
Author Dropdown Button (working) :
<div class="dropdown">
<button class="btn btn-outline-dark btn-sm dropdown-toggle mr-2" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
#if($request == '/posts'
OR strstr($request, 'posts?search=')
OR strstr($request, 'posts?page='))
Author
#else
#foreach($users as $user)
#php
if(strstr($request, 'user_id='.$user->id)){
echo $user->name;
}
#endphp
#endforeach
#endif
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
#foreach($users as $user)
<a class="dropdown-item" href="/posts?user_id={{$user->id}}">
{{ $user->name }}
</a>
#endforeach
</div>
</div>

Correct #If syntax in the blade view for Laravel 5.2

This is my code in the blade view. My purpose is for this specific HTML to show up only if the authenticated user's email address matches in the if condition. Please check below, it is currently not working as it should be
#if ('{{ Auth::user()->email }}' == 'evangelism.sec#gmail.com')
<li>
<i class="fa fa-file-text" aria-hidden="true"></i>File Uploads
</li>
#endif
Use without ' quotes and without {{ brackets just use Auth::user()->email or better \Auth::user()->email.
#if (\Auth::user()->email == 'evangelism.sec#gmail.com')
<li>
<i class="fa fa-file-text" aria-hidden="true"></i>File Uploads
</li>
#endif
Maybe this can work:
#if (Auth::user()->email == 'evangelism.sec#gmail.com')
//your code after authentication
#else
//user not logged in
#endif

laravel - if else loop not working proper

I'm trying to write an if-else loop: if there is an embed saved in the database it should display a calendar, if not it should display a button saying "add new calendar/add new embed", but it's not working. I can't figure out why. When there is an embed saved in the database it displays the embed but, if there isn't an embed it doesn't display the button to add a new calendar/embed:
Blade view:
#if (empty($calendars))
#else
<h4>No calendar settings</h4><br>
<a href="{{ route('calendarsettings.create')}}">
<button class="btn btn-success btn-cons m-b-10" type="button">
<i class="fa fa-cloud-upload"></i>
<span class="bold">Add embed</span>
</button>
</a>
#endif
#foreach($calendars as $calendar)
{{ $calendar->embed }}
#endforeach
Controller:
public function kalendarz() {
$calendars = Calendar::with('users')->where('user_id', Auth::user()->id)->get();
return view('kalendarz',['calendars'=>$calendars]);
}
In Laravel 5.4, I would probably use the #unless blade directive, that will show the block, unless a condition is met. What the following code would acheive is "show the entries if we have some, and show the button unless we have some entries".
#if($calendar)
// show entries
#endif
#unless(empty($calendars))
// show a button to add a new one
#endunless
It kind of is the same "logic" as with an else, but somehow I think it's more clear and easy to read than an #else statement, and make it separate from the if.
The correct syntax is:
#if(empty($calendars))
<h4>No calendar settings</h4></br>
<a href="{{ route('calendarsettings.create')}}">
<button class="btn btn-success btn-cons m-b-10" type="button"><i class="fa fa-cloud-upload"></i> <span class="bold">Add embed</span></button>
</a>
#else
#foreach($calendars as $calendar)
{{ $calendar->embed }}
#endforeach
#endif
empty() functions empty the variable $calendars, so try using count()
#if(count($calendars) > 0)
<h4>No calendar settings</h4></br>
<a href="{{ route('calendarsettings.create')}}">
<button class="btn btn-success btn-cons m-b-10" type="button"><i class="fa fa-cloud-upload"></i> <span class="bold">Add embed</span></button>
</a>
#else
#foreach($calendars as $calendar)
{{ $calendar->embed }}
#endforeach
#endif
When using Eloquent to get results from a database you get an instance of Illuminate\Database\Eloquent\Collection. To check if that collection is empty you use $collection->isEmpty() or $collection->isNotEmpty() (L5.4).
#if ($calendars->isNotEmpty())
// Have entries.
#else
<h4>No calendar settings</h4></br>
<a href="{{ route('calendarsettings.create')}}"><button class="btn btn-success btn-cons m-b-10" type="button"><i class="fa fa-cloud-upload"></i> <span class="bold">Add embed</span>
</button></a>
#endif
#foreach($calendars as $calendar)
{{$calendar->embed}}
#endforeach

How to put HTML inside a <td> in Laravel Blade?

I want to put Bootstrap Icon inside of my blade..But the problem is i couldn't get the actual output..Means here is the sample code:
#if (($row->is_management) === 1)
<td> <span class="glyphicon glyphicon-ok"></span> </td>
#else
<td><span class="glyphicon glyphicon-remove"></span></td>
#endif
Means if status is 1 then green checked will come and in another condition red cross.
Can anyone suggest me what the actual mistake here?
It will work on bootstrap css with glyphicons fonts. Please make sure that u have bootstrap css called and have glyphicons fonts
#if (($row->is_management) == 1)
<td> <span class="glyphicon glyphicon-ok"></span> </td>
#else
<td><span class="glyphicon glyphicon-remove"></span></td>
#endif
Try this, use == instead of ===
Updated Code:
#if (($row->is_management) == 1)
{{ <td> <span class="glyphicon glyphicon-ok"></span> </td> }}
#else
{{ <td><span class="glyphicon glyphicon-remove"></span></td> }}
#endif

if else condition in blade file (laravel 5.3)

I want to check if/else condition in my blade file. I want to check the condition $user->status =='waiting' as the code given below. Output returns correctly as I expected. But along with my output I caught curly braces {} printed. I want to remove curly braces in result. Is there anything wrong in my if condition ?
#if($user->status =='waiting')
{
<td><a href="#" class="viewPopLink btn btn-default1" role="button" data-id="{{ $user->travel_id }}" data-toggle="modal" data-target="#myModal">Approve/Reject<a></td>
}
#else{
<td>{{ $user->status }}</td>
}
#endif
No curly braces required you can directly write
#if($user->status =='waiting')
<td><a href="#" class="viewPopLink btn btn-default1" role="button" data-id="{{ $user->travel_id }}" data-toggle="modal" data-target="#myModal">Approve/Reject<a></td>
#else
<td>{{ $user->status }}</td>
#endif
I think you are putting one too many curly brackets. Try this
#if($user->status=='waiting')
<td>Approve/Reject </td>
#else
<td>{!! $user->status !!}</td>
#endif
You don't have to use braces in blade conditions, so your code would be like this:
#if($user->status =='waiting')
<td>
<a href="#" class="viewPopLink btn btn-default1" role="button" data-id="{{ $user->travel_id }}" data-toggle="modal" data-target="#myModal">
Approve/Reject
<a>
</td>
#else
<td>
{{ $user->status }}
</td>
#endif
If wants to assign value on basis of if else inside blade.
#if($shipping_fee >= 1)
#php
$shipping_fee = $any_variable1
#endphp
#else
#php
$shipping_fee = $any_variable2
#endphp
#endif

Categories