Ramzan Mahmood
Ramzan Mahmood

Reputation: 1901

redirect a against different requests (laravel 5.3)

I am working on laravel 5.3 I want to redirect a user on different pages again from which page user is coming . I have 3 pages one is

create

view all

view

I am using create page for create and edit purpose. view all and view both page have edit button. after click it goes in Controller edit method and redirect to edit page in both cases.

Edit page have a return button. I want when user click on return it should go to view all if he comes from view all page to edit other wise if he came form view it should go back to view when he click on return.

Code for edit button is as in both view and view all pages is as

    <li>
      <a href="{{ route('student.edit', $student->id) }}"> Edit</a>
    </li>

on click edit it comes here in controller edit method

public function edit($id){
    $student = Student::findOrFail($id);
    $data = ['action' => 'edit'];

    return view('student.create')->with(compact('data', 'student'));
 }

Buttons on create page are as right now

<div class="row">
  <button type="submit" value="dd">Save and add</button>
  @if ($data['action'] == 'edit')
  <a href="{{ route('student.index') }}">
     <button type="button">Return</button>
  </a>
  <button type="submit" value="edit">Save</button>
  @endif
</div>

Now its returning me to view all students fine from update method because i use check there on button value as

if(Input::get('add')) {
   return redirect()->route('student.create');
}else{
   return redirect()->route('student.index');
}

How can i redirect to student.view on base of that user cam from student view to edit

Any idea how can i do it may be i also can work with session please help how to do

Upvotes: 0

Views: 94

Answers (3)

vallabh
vallabh

Reputation: 21

Use {{ URL::previous() }}

you can update your code like below

<a href="{{ URL::previous() }}"><button type="button">Return</button></a>

Upvotes: 0

omitobi
omitobi

Reputation: 7334

To return to the previous page/route in your controller use:

return redirect()->back();

Or simple:

return back();

Then you can have edit function like this:

public function edit($id)
{
    $student = Student::findOrFail($id);
    $data = [ 'action' => 'edit'];

    return back()->with(compact('data', 'student'));
}

Ref: Laravel 5.2 doc - redirect

UPDATES:

If you mean it should go back after clicking the Return button then this should be enough:

 <a href="{{ URL::previous() }}"> Return </a>

PS: I tested this on Laravel 5.2.* and it worked I hope this helps :)

Upvotes: 0

jeremykenedy
jeremykenedy

Reputation: 4275

You can get the URI on the request using $uri = $request->path(); then use a switch the checks against $uri to dictate what to do next.

You will also need to modify your controllers head with

use Illuminate\Http\Request;

and update the edit method to public function edit(Request $request, $id){.

https://laravel.com/docs/5.3/requests#request-path-and-method

Upvotes: 1

Related Questions