How to pass a VueJS variable into a Laravel blade route - php

I'm looping over a JSON array in VueJS and outputting each item to the screen but I need to create a link/route to a resource controller with the ID being returned for each row like so:
<tr v-for="item in searchResults.group">
<td>
<button type="button" class="btn btn-info btn-sm">Edit</button>
So I've tried putting the variable into the route like so #{{ item.id }} but get the error:
syntax error, unexpected '{' (View: /application/resources/views/admin/edit.blade.php)
The way I have done it is not the correct way obviously but I can't find anything in the documentation to achieve this.
EDIT:
Further input on this. The route function requires a second parameter, in this case, the ID of the item to edit. In pure PHP/Blade I have this and it works:
<button type="button" class="btn btn-info btn-sm">Reduce</button>
For the dynamic search page, I need to somehow pass that second parameter into blade/php from a vuejs variable but I just can't work out how to do it.

You can try this using backtick.
Its works for me. I hope it's helpful for you.
<a :href=`{{ route('admin.edit', '') }}/${item.id}`>

You are mixing two concepts that won't go together like this.
The blade template is rendered server-side, whereas the parts of your vue.js related markup will be parsed on the client side (e.g. "browser").
Because of that, referencing a property of item within a blade expression will fail (as it does).
<a href="{{ route('admin.edit', #{{ item.id }} ) }}"
route(...) refers to an expression that is related to blade, an item is supposed to be a part of your vue.js app.
What you need to do is to dynamically create the link for editing the ressource within your vue.js app once you loop your searchResult.group.
You could achieve this by injecting a "route-template" for editing the ressource into your vue.js app and bind the href property to a vue.js method, like this:
<tr v-for="item in searchResults.group">
<a v-bind:href="getEditLink(item.id)">Edit</a>
....
The according method getEditLink(id) then assembles the actual link based on the given id of item and the provided "route"-template.

You can try by appending the id to the end like so:
{{ route('admin.edit') }}/#{{ item.id }}
I'm guessing you are following a REST URI guides so that would work for you.

Use Like this
Click

Related

Appending to a Laravel route in an href with JS

I have the following route in my div tag.
<div class="clickable-element col-lg-3 col-md-6 location-tile js-
location-id" data-href="{{ route('report.file.details', 'id') }}">
<div>Content</div>
</>
How can I use JS to append the end of the route?
My JS builds an object and with the location id name etc. This is what I have but I receive an error saying I'm missing a parameter in my route.
$LocationTile.find('.js-location-id href').attr('href').append(this.locationid);
The attr tag does not inherently get data elements, only the data tag does that. You need to start by changing .attr('href') to .attr('data-href') or .data('href') depending on version of Jquery you are using.
Also, can you share what LocationTile is. Is it another Jquery element? As well as what this represents. Are you looping elements?

As a result I redirected to my ID editing form using twig and php

I am learning to use twig and I do not know how to redirect to an editing path through ID
In pure PHP it would be like this for example:
edit
In twig to redirect to a location I use my route and the URL filter for example:
Back
How do I achieve this: editar but using twig.
First make sure you have defined the route with the parameters in your routing file.
So if you want to generate url for the route: /update.php?id={id} then you can pass the id parameter like this:
{{ url('update.php', {'id': '. $row['id'] .'}) }}
Documentation

Laravel 5 Can't send data from url

Hi Im currently trying to make a basic crud for my questions resource on laravel 5
so far so good, but now Im having troubles displaying the edit view, because the url is not being created correctly when I try to send the resource id in the url
here's the anchor Im using
<button class="submit-form button button btn btn-primary" style="margin: 0 1em;" type="submit">Editar</button>
here's the route in my routes file
Route::get('admin/preguntas/editar/{id}','QuestionsController#edit')->name('admin/questions/update');
the method in the controller works just fine, when I manually type this url
/admin/preguntas/editar/4
It shows the view without problems, but when I go from the anchor the url it goes is this one
/admin/preguntas/editar?4
of course the 4 is the id from my resource, but why is not typing the correct url?
thanks in advance
You can't wrap a hyperlink around a button so my assumption is your problem is related to the form action (since button type is submit), not the a href.
Since it appears you're using bootstrap, there is no need to use a button to get the styling of a button.
<a class="btn btn-primary"> will work just fine.
And your "QuestionsController#edit" accepts "id" argument?
like
function edit($id){}
And I think, your are using wrong route link helper
Route::get('user/{id}/profile', function ($id) {
//
})->name('profile');
$url = route('profile', ['id' => 1]);
First write proper link button as Devon said:
Editar
Second you must write proper route name format:
Route::get('admin/preguntas/editar/{id}','QuestionsController#edit')->name('admin.questions.update');
Hope it will help you to solve the problem :)

Laravel 5.3 url pass query string but not appearing on calling

I have grid with multiple column and one of it for me to press the edit button so I can pass the id and on the route I will call the sql and get the relevant values and call the edit view.
Below is my codes how I pass the id value.
<td><a href="{{url('/edittestdetails/',{{$test->ID}})}}>Edit</a></td>. I prefer the url to be edittestdetails?id=value? but now nothing is appearing when I call the url the paramereter is not appearing.
The correct syntax is:
<a href="{{ url('/edittestdetails/', $test->id) }}>Edit</a>
{{}} will be converted to the echo() clause, so you can't use {{}} inside another {{}} construction.
Also, property name is id by default and not ID.
<td>Edit</td>

Laravel back button

I am trying to create a simple back button on a page. The user can arrive to this page from two different pages so I would like to know from which page he arrived. Is that possible?
In Laravel, you can do something like this: Back (assuming you're using Blade).
Laravel 4
{{ URL::previous() }}
Laravel 5+
{{ url()->previous() }}
Laravel documentation
I know this is an oldish question but I found it whilst looking for the same solution. The solution above doesn't appear to work in Laravel 4, you can however use this now:
Go Back
Hope this helps people who look for this feature in L4
(Source: https://github.com/laravel/framework/pull/501/commits)
Laravel 5.2+, back button
Back
Indeed using {{ URL:previous() }} do work, but if you're using a same named route to display multiple views, it will take you back to the first endpoint of this route.
In my case, I have a named route, which based on a parameter selected by the user, can render 3 different views. Of course, I have a default case for the first enter in this route, when the user doesn't selected any option yet.
When I use URL:previous(), Laravel take me back to the default view, even if the user has selected some other option. Only using javascript inside the button I accomplished to be returned to the correct view:
Voltar
I'm tested this on Laravel 5.3, just for clarification.
The following is a complete Blade (the templating engine Laravel uses) solution:
{!! link_to(URL::previous(), 'Cancel', ['class' => 'btn btn-default']) !!}
The options array with the class is optional, in this case it specifies the styling for a Bootstrap 3 button.
On 5.1 I could only get this to work.
Back
You can use javascript for this provblem.
It's retrieve link from browser history.
<script>
function goBack() {
window.history.back();
}
</script>
<button onclick="goBack()">Go Back</button>
One of the below solve your problem
URL::previous()
URL::back()
other
URL::current()
You can use {{ URL::previous() }}
But it not perfect UX.
For example, when you press F5 button and click again to Back Button with {{ URL::previous() }} you will stay in.
A good way is using {{ route('page.edit', $item->id) }} it always true page you wanna to redirect.
<i class="fa fa-angle-left"></i> Continue Shopping
This worked in Laravel 5.8

Categories