Unexpected token '{' when passing prop to vue component from blade - php

I need to pass a variable from a blade to a vue component, usually I am able to do something like
<vue-component :the-prop="{{ blah }}" />
when passing prop values. However in this case I'm trying to pass a value from $_SESSION, and I am getting an invalid token error and am not sure how to get around it.
I've tried:
<vue-component :the-prop="{{ $_SESSION['data'] }}" />
and
<vue-component :the-prop="{{ session('data') }}" />
and it seems never to even get to the "session" portion and gives me an error:
Invalid expression: unexpected token '{'
the code works fine if i hard-code an id like so:
<vue-component :the-prop="3" />
and typing out v-bind:the-prop makes no difference

If you're doing this from a blade template file, you should remove the colon so that the value is not binded. Blade itself should plug in the variable value from your {{ }} syntax, so there is no binding needed on the vue side of things.

In a component, prefixing an attribute with a colon means you want it handled directly by PHP, as described in the documentation. So instead of this:
<vue-component :the-prop="{{ $_SESSION['data'] }}" />
Try this:
<vue-component :the-prop="$_SESSION['data']" />
Or, even better, this:
<vue-component :the-prop="session('data')" />

Related

How to pass a string to a blade component?

As an example there is this component:
resources/views/components/example.blade.php
<div>
#if($foo === "bar")
Bar
#else
Foo
#endif
</div>
that I render like
#php
$foo = "bar";
#endphp
<x-example :foo="$foo" />
what is working.
But to have my code looking cleaner, I want to pass the string directly to the component, but the following I tried are not working:
<x-example :foo="bar" />
<x-example :foo='bar' />
<x-example :foo="\'bar\'" />
<x-example :foo="'bar'" />
<x-example :foo=""bar"" />
Easy mistake to make, as mentioned in the Laravel documentation on passing data to components;
You may pass data to Blade components using HTML attributes.
Hard-coded, primitive values may be passed to the component using
simple HTML attribute strings. PHP expressions and variables should be
passed to the component via attributes that use the : character as a
prefix
So update how you use the Blade component as below:
<x-example foo="bar" />

HTML Input name attribute as array - how to access values in laravel

I've recently used array notation when naming html input fields. e.g.
<input type="text" name="user[$userId][licenseStatus]">
I've never used this syntax before and although it is extremely handy I cannot see a good way to access the data held in session from the view when using Laravel.
For instance I might want to retrieve the old data back into the input like this when say a validation failure occurs:
<input type="text" name="user[$userId][licenseStatus]" value="{{session()->getOldInput(user[$userId][licenseStatus], '')}}">
But this obviously doesn't work because the array syntax on the name field means the data is held in an array in session like this:
[
_old_input => user[
32=>licenseStatus = 'xyx',
12=>licenseStatus = 'xyz'
]
]
So is there a smart way to retrieve old input values?
Thanks,
If validation error occurs, in the controller do a redirect with input values. E.g.:
return redirect('form')->withInput();
Then in the form itself, you can put the form value like this:
<input type="text" name="user[$userId][licenseStatus]" value="{{ old('user.$userId.licenseStatus') }}">
You can double check in the laravel documentation: https://laravel.com/docs/8.x/requests#flashing-input-then-redirecting

Laravel: variable + parameter in view

I want to ask if it is possible to do something like this in View in Laravel 5.2:
<p> This is window: {{$element_ + 'window'}} </p>
<p> This is wall: {{$element_ + 'wall'}} </p>
The values for this variables are from $element_window, $element_wall.
There are couple of options.
First - is to use #php block in .blade file for dynamic output:
#php
${'window'} = ${$element_.'window'}
#endphp
Second is to write custom blade extension to output anything you need.
Third is to define custom method in your Model (if you use one).
However I should mention, that such variable assignment inside template (first option) is not recommended. It's hardly readable and could cause Exceptions if such dynamically created variables do not exist at some point. Not saying that this is not presentation logic.
If you want to dynamically name a variable.. you can do the following.
<p> This is window: {{ ${'element_'.'window'} }} </p>
<p> This is wall: {{ ${'element_'.'wall'} }} </p>
That should work.
But if just want to concatenate a string to the variable... you can use "." :-)

Trying to submit a form path and how to pass form values to URL

I just got into Symfony2 as an ASP.NET programmer. I have this very simple form that I want to put in. Since it only contains a label and an input, I figured I didn't want to make a separate form class for it. But as soon as I tried to preform that, I ran into some trouble:
<form action="{{ path('search', [{'src':'bla'}]) }}" method="get">
<label for="src"></label>
<input type="text" name="src" id="src" class="form-control search-input" placeholder="Type a group or song name: ie. Metallica, Fly me to the moon etc.">
</form>
How do I set the path to mydomain.com/search/{src}, I'm trying to at least build a path for now, so I'm using 'bla' as some placeholder valye. Right now from the code above, I receive the following error upon loading the page that contains the form:
An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("src") to generate a URL for route "search".") in :home:index.html.twig
Some methods I also tried:
path('search', 'bla') <-- and --> path('search', 'bla', [])
brings error:
An exception has been thrown during the rendering of a template ("Warning: array_replace(): Argument #3 is not an array") in :home:index.html.twig
path('search', ['src':'bla'])
and some more (probably) nonsense that didn't get me close to the result.
Is this even doable, or do I have to do it the 'normal' way with {{ form_start() }} and form classes?
Don't try, just read documentation, it's more effective and faster :)
http://symfony.com/doc/current/book/templating.html#book-templating-pages
{{ path('search', {'src':'bla'}) }}

Laravel get route with parameter in view

I need to get the route in my view for redirecting.
Right now I'm doing this:
Laravel 4 - Get Current Route Name on Hidden Input to use for search
{{ Form::hidden('route', Route::current()->getUri()) }}
Problem is, it looks like this when I get on a page with an id:
<input name="route" type="hidden" value="recipes/details/{id}">
How can I parse the {id} variable?
You should use:
Request::url();
Instead of Route::current()->getUri(), but it's not proper way to redirect from the View, you should redirect from your Controller instead.
It should be in your case (for full url):
// 'http://example.com/recipes/details/10'
{{ Form::hidden('route', Request::url()) }}
or use this (only for path):
// 'recipes/details/10'
{{ Form::hidden('route', Request::path()) }}

Categories