Laravel back button - php

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

Related

How to pass a VueJS variable into a Laravel blade route

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

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 :)

Dropdown with 'Delete' Request in Laravel? [duplicate]

This question already has answers here:
laravel method in href link?
(4 answers)
Closed 6 years ago.
I allready asked something like that, but haven't got something that helped me out. So I'm sorry for asking again..
First I've created a Form with a submit button that directs me to my delete route. The Form had a 'DELETE' as a method.
{!! Former::danger_submit('Delete') !!}
{!! Former::horizontal_open()->method('DELETE')->action(action("Test\\TestController#destroythread", $comment->id)) !!}
{!! Former::close() !!}
This worked perfect. That means my controller function, the routes and the form haven't any mistakes.
Now I decided, that I want a dropdown. In the dropdown, there should be written: delete and edit. Lets focus on the delete.
The dropdown links looking like this:
<ul class="dropdown-menu">
<li>Edit</li>
<li>Delete</li></ul> // <- Delete
I can't put my Laravel form inside this <li> tag of course.. Thats why I tried to do a link that directs me to my laravel delete route.
Like this:
<li>Delete</li>
but this haven't worked because this gives me a 'GET' request, not a 'DELETE'.
Well, now I'm stuck. I'm really sorry for asking this again..
I hope someone can help me out.
My laravel Delete Route is this:
Route::delete('/show/{id}', ['as' => 'destroythread', 'uses' => 'Test\\TestController#destroythread']);
and my Controller function is this:
public function destroythread($id) {
$comment = Comment::query()->findOrFail($id);
$comment->delete();
return redirect()->back();
}
My last question about this problem couldn't help me out. A user said I can use ajax but I don't really understand how that works and don't really liked this way at all.
Thanks for any help!
Edit by #Joost answer:
{!! Former::horizontal_open()->method('DELETE')->action(action("Test\\TestController#destroythread", $comment->id)) !!}
<li><button>Delete</button></li>
{!! Former::close() !!}
worked but now there is a real button in my dropdown.. this don't look good. Is there any way that there just will stand a: Delete ?
You're saying that your form works. So you could just use a link instead of a button and simple JS like:
<a href="#" onclick="document.yourFormName.submit(); return false;">
Or you could try:
<a href="#" onclick="parentNode.submit(); return false;">

Issue with Laravel 4 Routes?

I'm trying to learn laravel, but I'm running into an issue with routing.
I have the following in my Routes file:
Route::get('home', function()
{
return View::make('home');
});
Which works if I type
http://localhost/laravel/public/home
However, on a another page I have a form, that when submitted should take me to that page like so:
{{ Form::open(array('url' => 'home')) }}
Now this takes me to the correct address, but throws an exception. But, if I reload the page with the same URL then the page loads correctly. So what is the issue here? Is there a problem with the way my form is set up?
Route::get is when you want to issue a get request to the page, to get the contents basically. If you want to post to a page, you need to do, in addition to your Route::get,
Route::post
Another option that people will tell you is:
Route::any
but I would advise staying away from this because the logic will probably be different for the two routes.

How to see post back data on any view page in laravel which uses blade template system?

How can I print post back data on any view page in Laravel framework which uses blade template system.
$_POST['state'] is not giving any results.
Laravel encapsulates the whole request data in the Input Facade, so you just have to:
State: {{ Input::get('state') }}
or, if you're trying to read data from a redirected request:
State: {{ Input::old('state') }}
You can use conditionals this way in Blade:
#if(Input::get('state'))
<button ...>
#endif

Categories