How to get data from previous page in laravel?

Member

by aubrey , in category: PHP CMS , 3 months ago

How to get data from previous page in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lew , 3 months ago

@aubrey 

In Laravel, you can use the back() function to redirect back to the previous page, along with any data that was submitted on the previous page.


Here is an example of how you can get data from the previous page in Laravel:

  1. In your controller method on the first page, store the data you want to pass to the next page in the session:
1
2
3
4
5
6
7
8
public function store(Request $request)
{
    $data = $request->all();

    session()->put('data', $data);

    return redirect()->route('next.page');
}


  1. In the controller method on the next page, retrieve the data from the session:
1
2
3
4
5
6
public function show()
{
    $data = session()->get('data');

    return view('nextpage', compact('data'));
}


  1. In your view page, you can access the data like this:
1
2
3
@if(isset($data))
    <p>{{ $data['key'] }}</p>
@endif


By following these steps, you can easily get data from the previous page in Laravel.