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
Related
I am trying to write some code inside a laravel blade as shown below
#can('identical_trademark_local_show')
#php
$trimmed_payment_id=Str::substr($Payment->payment_id,0,9);
if($trimmed_payment_id=='AlYAFI:IL')
{
<a class="btn btn-xs btn-primary" href="{{ route('admin.identical-trademark-locals.show', $Payment->id) }}">
trans('global.view')
</a>"
}
if($trimmed_payment_id=='AlYAFI:SL')
{
<a class="btn btn-xs btn-primary" href="{{ route('admin.similarlocals.show', $Payment->id) }}">
trans('global.view')
</a>
}
if($trimmed_payment_id=='AlYAFI:SI')
{
<a class="btn btn-xs btn-primary" href="{{ route('admin.similarinternationals.show', $Payment->id) }}">
trans('global.view')
</a>
}
#endphp
#endcan
The problem is that am getting a syntax error
syntax error, unexpected '<'
You can't use the mustache {{ }} symbol inside a #php block and you need to write your HTML code outside of your #php block
You'd be better off writing everything in blade syntax, one of the way to do it is as follow:
#can('identical_trademark_local_show')
#php
$trimmed_payment_id=Str::substr($Payment->payment_id,0,9);
#endphp
#if($trimmed_payment_id=='AlYAFI:IL')
<a class="btn btn-xs btn-primary" href="{{ route('admin.identical-trademark-locals.show', $Payment->id) }}">
trans('global.view')
</a>
#endif
#if($trimmed_payment_id=='AlYAFI:SL')
<a class="btn btn-xs btn-primary" href="{{ route('admin.similarlocals.show', $Payment->id) }}">
trans('global.view')
</a>
#endif
#if($trimmed_payment_id=='AlYAFI:SI')
<a class="btn btn-xs btn-primary" href="{{ route('admin.similarinternationals.show', $Payment->id) }}">
trans('global.view')
</a>
#endif
#endcan
NOTE
You should avoid the #php block and instead pass on the value from your controller.
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>
I need use angulaar data like parameter in a Blade form, but i donĀ“t know how to do it
This is my code
Sorry for my English
<tr ng-repeat="data in datos">
<td>{{data.PAGO}}</td>
<td>{{data.AUTORIZACION}}</td>
<td>{{data.FECHAYHORA}}</td>
<td>
<div class="btn-group">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li>{!! Form::open(['route'=>array('movimientosC.destroy',//I need angular data here//),'method'=>'DELETE']) !!}
<input type="hidden" name="accion" value="1"><div class="container">{!!Form::submit('Cancelar',['class'=>'btn btn-danger btn-xs']);!!}
</div>{!! Form::close() !!}</li>
</ul>
</div>
</td>
</tr>
You can either change angular brackets through use of $interpolateProvider:
angular.module('myApp', []).config(function($interpolateProvider){
$interpolateProvider.startSymbol('{[{').endSymbol('}]}');
});
Or you can change laravel blade tags:
Blade::setContentTags('<%', '%>');
Blade::setEscapedContentTags('<%%', '%%>');
You can do this in blade by using the syntax #{{ data.PAGO }}. The # tells blade it should render that as text.
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
My view contains the following code
#foreach ($building as $maps)
<div class="btn-group">
<a class="btn btn-primary" role="button" href="{{ URL::asset('floorPlans/$maps') }}" aria-expanded="false" aria-controls="collapseExample">
{{ $x }}
</a>
</div>
<?php $x++; ?>
#endforeach
Yet this is resulting in the follwoing HTML
<div class="btn-group">
<a class="btn btn-primary" role="button" href="http://facilities-lara.hslc.wisc.edu/floorPlans/$maps" aria-expanded="false" aria-controls="collapseExample">2</a>
</div>
With $maps still being appended to URL instead of the value within $maps. Is it possible to retrieve the value within a blade template?
Thanks,
Otterman
Everything between the blade tags is just normal PHP. So you can simple concatenate the string and the variable:
{{ URL::asset('floorPlans/'.$maps) }}