Display image which is stored in database in Laravel - laravel

I want to display image of the logged in user using session in Laravel but I am an getting error. And I think it may be due to quotations.
This is my code:
<img src="{{URL::asset('img/'".{{Session::get('admin-pic')}}."'')}}" class="img-circle"/>
Error:
Parse error: syntax error, unexpected '".{{Session::get('admin-pic'))' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ')' (View: /opt/lampp/htdocs/laravel/blog/resources/views/admin-profile.blade.php)

You don't need to use {{}} inside another one, actually you can't:
<img src="{{URL::asset('img/'.Session::get('admin-pic'))}}" class="img-circle"/>

{{}} will be rendered to echo and you can't nest echo. Also, you can just use the asset() and session helpers. So, the correct syntax:
<img src="{{ asset('img/' . session('admin-pic')) }}" class="img-circle"/>

Related

Laravel 5.8 blade, getting values from nested objects error

I am getting some weird error while working with blade.
It works just like it should when getting the data inside a dd(), but
when calling it without a dd(), an error is thrown telling me that it can't find a field in my object.
In my blade file, this works just fine:
{{ dd($p->components->image->lg_img) }}
This doesn't:
<img src="{{ $p->components->image->lg_img }}" />
The error:
Undefined property: stdClass::$image (View:
/home/vagrant/code/xxxxxx/resources/views/resources.blade.php)
The solution is:
check if the variable exists
<img src="{{ !empty($p->components->image->lg_img) }}" />
This problem happens because some attributes might not exist.

Laravel Blade Vue - Invalid or unexpected token

I am trying to pass a variable from Blade to a Vue component prop. However, when I'm passing the variable, something goes wrong. The error which I receive is the following:
[Vue warn]: Error compiling template:
invalid expression: Invalid or unexpected token in
[{"id":6,"name":"aaa","created_at":"2019-12-05
Raw expression: :companies="[{"id":6,"name":"aaa","created_at":"2019-12-05"
153| <!-- Page Content -->
154|
155| <company_index :companies="[{"id":6,"name":"aaa","created_at":"2019-12-05" 14:35:38","updated_at":"2019-12-05="" 14:35:38"}]=""></company_index>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
156| <!-- END Page Content -->
157| </main>
(found in <Root>)
As you can see, a random quote is added which is NOT present in the datetime itself (after 2019-12-05). It only gets added when passing it to the Vue component. Here is my code from my blade file:
#extends('layouts.backend')
#section('content')
<!-- Page Content -->
<company_index :companies={{json_encode($companies)}}></company_index>
<!-- END Page Content -->
#endsection
I've tried the following things:
Checked to see if the datetime somehow had an extra quote, this was not the case.
Checked if other Eloquent Models had the same problems, which they do (They also use standard datetimes from Laravel).
Remove json_encode from my blade file
Replaced :companies={{json_encode($companies)} with :companies={!! $companies !!}
Searched for people with the same problem, but did not find anything about this problem.
I don't think there is anything wrong with my code, but I don't know for sure what my problem is. If you need more details, just ask.
try adding double quote :companies="{{json_encode($companies)}}"

Laravel Collective Syntax

I'm learning laravel (5.5) from a book.
I installed laravel collective, the book says, to link a css file write like this:
{{!! HTML::style('css/app.css') !!}} It works fine.
but in the output two empty {} braces appear (because they are not being used as part of the syntax). So, I removed them and it still works fine.
Question is which syntax is correct?
this {{!! HTML::style('css/app.css') !!}}
or {!! HTML::style('css/app.css') !!}
???
Out of the box Laravel tries to help you with security within your apps. When outputting data using the {{ $foo }} data, Laravel automatically calls the htmlspecialchars() method to prevent XSS attacks. In some cases you do want or need to output HTML, that's why Laravel created a separated syntax for that: {!! $foo !!}.
So to answer your question: {!! $foo !!} is the correct syntax.
Off topic:
If you'd like to not use the unescaped data syntax, you could write you're CSS linking like this:
<link rel="stylesheet" href="{{ asset('css/app.css') }}">

Raw HTML with Laravel and send paramaters/var

I'm trying to implement the KeeWeb HTML file to my Laravel project.
I've tried to just add it to a blade file (e.g. keeweb.blade.php) and then using return view('keeweb'); and I'm getting the following error: Parse error: syntax error, unexpected '=', expecting ',' or ')'
However, I've also opened the HTML file with my browser and it works fine, so it seems that Blade is parsing something it shouldn't be. Fine.
So next I tried to just make a test blade file, and in that just #include('keeweb') and then rename keeweb.blade.php to keeweb.php and I get the same.
So next I tried to simply return view('keeweb'); //keeweb.php with the same error.
I need to parse some sort of variable because I need to change the head of the document so I cannot just File::get()
You are right, there are multiple occurrences of {{ in that file which Blade is trying to parse out.
I don't think you want to rename it to have only .php extension though. Try keeping it keeweb.blade.php and when you include it, try the following...
#verbatim
include('keeweb')
#endverbatim
Or even placing the #verbatim and #endverbatim inside the actual keeweb file at the top and bottom respectively, then you should be able to include it just like any other blade file.

Laravel 5 Captcha in Laravel 5 is not visible

I use Laravel 5.1 and this captcha package: https://github.com/mewebstudio/captcha.
When using this: {{Captcha::img()}} in my blade.php it doesn't show me the captch image. It just prints me out the following html:
<img src="http://localhost/myproject/public/captcha/default?xyD4gHW2" alt="captcha">
Any ideas why? Thank you
do this:
{!! Captcha::img() !!}}
from the docs:
https://laravel.com/docs/5.2/blade
By default, Blade {{ }} statements are automatically sent through
PHP's htmlentities function to prevent XSS attacks. If you do not want
your data to be escaped, you may use the following syntax:
Hello, {!! $name !!}. Note: Be very careful when echoing content that
is supplied by users of your application. Always use the double curly
brace syntax to escape any HTML entities in the content.

Resources