Laravel Blade Vue - Invalid or unexpected token - laravel

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

Related

$quot; not being parsed in laravel email

For some reason, Laravel does not parse " as expected in the footer of emails.
I am expecting to see an actual quote, but it's returning the HTML as-is.
I have tried overriding this using the toMailUsing in my AuthServiceProvider but this particular line doesn't seen to be editable.
Please advise on what I'm doing wrong.

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

Laravel Nova not loading any resources, blade error

Nova was working for me before. I started working on the front-end, and when coming back to Nova it suddenly doesn't work anymore. I can log in, but then it shows the loading animation for all resources and it's not loading data.
I get this error:
Trying to get property of non-object (View: longpath/location.blade.php)
In location.blade.php
#extends('app')
#section('title')
{{ $location->title }}
#endsection
#section('content')
#endsection
The weird thing is that on the front-end, location.blade.php loads perfectly fine, as I pass the $location variable in the LocationController. No errors and nothing in the error log. In LocationController:
$location = Location::
where('id', $this->location_id)
->first();
return view('location', [
'location' => $location
]);
So it shows the error, and this error is in the logs as well. If I comment out {{ $location->title }}, it doesn't show the error anymore, but it's still not loading any data, and nothing shows up in the error log. So I have no clue why it's not loading any data. It's also a mystery to me why a (front-end) Blade template would generate an error in Nova, while it works perfectly fine on the front-end.
Update:
If I comment out this specific route in routes/web, Nova works again. Not sure why this route impacts Nova?
Route::get('/{location_id}/{location_title}', 'LocationController#viewLocation');
If I add the route back in, in my console I get:
TypeError: Cannot read property 'length' of undefined
Your route is problematic because:
Route::get('/{location_id}/{location_title}', 'LocationController#viewLocation');
is going to catch any /foo/bar URL.
If you do php artisan route:list | grep nova you'll see all of Nova's routes, and you'll find a bunch in this format:
/nova-api/metrics
/nova-api/cards
/nova-api/search
/nova-api/{resource}
etc. etc. etc.
(In other words, a bunch of Nova's routes are being sent to your LocationController instead of the right Nova controllers.)
You may be able to fix this by taking the Nova::routes call out of the app/Providers/NovaServiceProvider.php file and putting it in your routes files directly, but the cleaner solution is likely to adjust your route to be something like /locations/{location_id}/{location_title} that's not going to conflict. Wildcarded top-level routes tend to cause issues like this.
You may also be able to do this:
Route::get('/{location_id}/{location_title}', 'LocationController#viewLocation')
->where('location_id', '[0-9]+');
This will make your route only activate for numeric IDs, which means it won't interfere with the non-numeric nova-api routes.

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