Get image name from database in laravel - laravel

I have database, with columns image and alttag. I want to use them in laravel blade view. I try something like this:
{{ HTML::image('images/{{ $item->image }}', $alt="{{ $item->alttag }}") }}
But syntax isn't correct. If i just echo image and alttag like this:
<h1>{{ $item->alttag }}</h1>
then they are correct. I wonder what is wrong in my code.

You used blade syntax in a PHP string. Watch the compiled blade templates to see where you did go wrong.
In short. Try this:
{{ HTML::image('images/'. $item->image, $alt = $item->alttag) }}
or equally short:
{{ HTML::image("images/{$item->image}", $alt = $item->alttag) }}

Related

Laravel Blade Helpers not pulling in columns with spaces

I have a table with many columns and some of those columns have spaces in their names (i.e. 'Provider First Name'). I know the syntax to use these in Blade helpers and am using this in other parts of my app: {{$provider->{'Provider First Name'} }}. This works fine in other parts.
I have the following in my ProviderController:
public function show($id)
{
$provider = NPIData::where('NPI', $id)->first();
$providers = NPIData::all();
return view ('profiles.provider', compact('provider', 'providers'));
}
I have brought in the NPIData Model to the controller.
I have the following in my provide.blade.php file:
#extends('layouts.profiles')
#section('content')
<div>
{{ $provider->NPI }}
{{$provider->{'Provider First Name'} }}
</div>
#endsection
Oddly, the NPI will pull in, but the 'Provider First Name' does not. I have tried many other columns with spaces and none of them work. I even copied and pasted from other parts of my app where the syntax to pull these in works and it does not work here.
Instead of:
{{$provider->{'Provider First Name'} }}
Try this:
#php
$providerFirstName = $provider->{'Provider First Name'};
#endphp
{{ $providerFirstName }}
UPDATE
If not you can always go with array access:
{{ $provider['Provider First Name'] }}

How to eval Blade variable when pulling variable from database

I am using Laravel 5.7 I am having a variable stored in the database and would like to have it evaluated by Blade before rendering.
Database
Field: name, Value: {{ $organisername }}
I am pulling out this piece of data into a Blade template like this.
<h4>1. Registration</h4>
<br />
{{ $job->organisername }}
Chrome then displays {{ $organisername }} instead of evaluating {{ $organisername }}.
1. Registration
{{ $organisername }}
If I type {{ $organisername }} into the Blade template, it gets evaluated to John when the page is rendered..
1. Registration
John
I'm using TinyMCE to input {{ $organisername }} into the textarea, and it gets saved into a Database, if that helps.
If by evaluate you mean to do some consistency for your variable, know that this Blade echo statement "{{ }}" already does that with htmlspecialchars, like explained in the docs:
https://laravel.com/docs/5.7/blade#displaying-data
Blade templates are compiled down into PHP before they evaluate. Essentially, blade compiles:
{{ $job->organisername }}
Into
<?php echo e($job->organisername); ?>
Once you understand this and how PHP evaluates variables, you should be able to understand why you can't set Blade constructs in the database and have them evaluated when it's printed.
It's basically the same as echoing a literal string:
<?php echo '{{ $organisiername }}'; ?>
You wouldn't want Blade or PHP evaluating any expression they come across as they print, that would potentially be very insecure.

Laravel blade creating url

I have a simple problem, basically I am getting name of the website from database and create a link according to it's name. it looks like:
#foreach ($websites as $website)
<a class="websites" href=" {{ asset ($website->name )}}"> {{ asset ($website->name )}}
</a>
#endforeach
Which gives for example: http://localhost/name
Howver links needs to be like this:
http://localhost/website/name how can I add /website into my URL using blade template in laravel?
Try this:
{{ url('website/' . $website->name) }}
This have some improvement on #Laran answer regarding best practices.
You would better use url parameters instead of concatenating the $name parameter
{{ url('website', [$name]) }}
And using named routes will be better to decouple the routing from the views.
// routes/web.php
Route::get('website')->name('website');
and write inside your {{ route('website', [$name]) }}

How to the domain url in laravel5

I have to get the domain url (ex. http://www.example.com/) in laravel blade. I've tried using {{ url() }} but it returns the path to my public directory. Is there any one line function to get this? How do I get the domain in blade? Need help. Thanks.
You can also try
{{ Request::server ("SERVER_NAME") }}
{{ Request::server ("SERVER_NAME") }}
Or go with
{{ Request::root() }}

About Laravel - How to use function string in view

i try to to show data from database article...the content is contain html tags and long text..
so i want make a read more and convert tag html to html view..
this is my code is run :
{{ HTML::decode($show->content) }}
{{ str_limit($show->content, $limit=100, $end=' ...') }}
i try this :
{{ HTML::decode(str_limit($show->content,$limit=100, $end=' ...')) }}
{{ str_limit(HTML::decode($show->content),$limit=100, $end=' ...') }}
but not show ( blank )
annyone can help me to fix it??
thank u b4
Is {{ $show->content }} returning any data to format?
Is your template is .blade?
Maybe it placed somewhere in HTML, where it is not visible?
Try to use this construction with some predefined string to find out if it works. Here is working example:
{{ str_limit('Some big text', $limit = 5, $end='...') }}

Resources