Laravel 5.8 blade, getting values from nested objects error - laravel

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.

Related

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

Display image which is stored in database in 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"/>

Laravel Blade Template passing data to Vue JS component

I am having a problem passing a property using Vuejs ~1.0 to a child component from a Laravel Blade template. If I pass something in plain text it works just fine, but when I try to pass a js property, array, or object it doesn't work at all. I currently have a blade file with a custom component which looks like this:
<my-component video="#{{ stuff }}"></my-component>
If I leave out the #{{ }} the only thing that will be passed is the string stuff, and if I leave out the #, I obviously get a blade error, but if I use #{{ stuff }}, then all I get is the string {{ stuff }}. I'm obviously missing something, but can't tell where I'm going wrong. Thanks in advance.
Look like I just figured it out, it seems that I was missing the colon before video, so it should have appeared like so:
<my-component :video="stuff"></my-component>
If you are passing a variable to the component, then use:
<my-component :video= "{{ json_encode($stuff) }}" ></my-component>
Don't forget the double quotes or the result would be unpredictable for things like objects.
If you are passing model then do this:
<my-component :video="{{ $stuff->toJson() }}" inline-template></my-component>

How to get submitted data in the same form in laravel

I am using laravel 5.0.
I have a form in my project. When I submit a form it return with error. But not getting the posted data.
I have used {{ old('name') }}. It is working with perfect. But I want to make a condition with the post data. If I use
#if( {{ old('name') }} == "praveen" )
it shows error.
I have seen Request::get('name') method one time. But I cannot remember that correctly. Please give me a solution.
Do not print in your condition. Just this:
#if(old('name') == "praveen")
// code here
#endif
to get submitted data in the same form you should use
return Redirect::back()->with('name');

Resources