Laravel: error with {{url()}} embedded in html - php

I want to do the following in my blade.php but I get a syntax error:
<a class="btn btn-info" role="button" href="{{ url('/Animal/{{$animal->id}}/edit')}}">Edit Profile</a>
The error is to do with the href attribute I think, how do I correct the syntax?
Error I am getting:
enter image description here

You have an error in your code, please try this one.
<a class="btn btn-info" role="button" href="{{ url('/Animal/' . $animal->id . '/edit')}}">Edit Profile</a>
Hope this helps.

Yes you have a syntax error in your code. You can't use {{ }} inside another {{ }}.
You have access to php variable inside this first {{ }}.
Use "" instead of '' to use php variables inside a string.
<a
class="btn btn-info"
role="button"
href="{{ url("/Animal/$animal->id/edit") }}"
>
Edit Profile
</a>

Related

How to refactor Php code inside a Laravel view blades

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.

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

Escaping trouble - Blade Template in Laravel 5.1

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) }}

How can I make a echo in echo in laravel with curly braces?

{{HTML::decode(link_to_route('showInsList','<button class="btn btn-primary" type=" button"><span class="badge">{{$test->num}}</span> Messungen </button>', array($test->id))) }}
I want to show the value of $test->num but it is not working.
What am I doing wrong? Please help.
If you're trying to simply generate a button with a link, it is much cleaner to just write the HTML and use the URL helper. Something like this:
<a href="{{ route('showInsList', array($test->id)) }}">
<button class="btn btn-primary" type="button">
<span class="badge">{{ $test->num }}</span>
Messungen
</button>
</a>
But, you aren't supposed to have button inside a tag, so you should consider styling the a tag to look like a button instead.
<a class="btn btn-primary" href="{{ route('showInsList', array($test->id)) }}">
<span class="badge">{{ $test->num }}</span>
Messungen
</a>
Try to stick to valid and semantic HTML to avoid headaches with browsers later on.
{{HTML::decode(link_to_route('showInsList','<button class="btn btn-primary" type=" button"><span class="badge">{{$test->num}}</span> Messungen </button>', array($test->id))) }}
You have already started echoing your string using the opening curly braces {{ the first time you've enter it. So you don't need to use the curly braces again on $test->num
Try again using the following
{{HTML::decode(link_to_route('showInsList','<button class="btn btn-primary" type=" button"><span class="badge"> $test->num </span> Messungen </button>', array($test->id))) }}
The {{}} surrounding $test->num are gone.

laravel put html in a tag with blade

I have this code in my view:
{{ HTML::linkRoute('login', 'Sign In', array(), array('class' => 'btn')) }}
The result is:
Sign In
However, I would like the result to be:
<a href="http://your.url/user/login" class="btn">
Sign In <i class='ff'></i> profile
</a>
So my question is how to do that in laravel?
Many thanks
I found the solution myself, which is:
<a href="{{ URL::to('foobar') }}">
<i class="fa fa-location-arrow"></i> profile
</a>

Categories