Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

djxprice's avatar

Options for Returning Views with Data?

Hi!

I have a controller function called via a route:

Route::get('/viewMap', ['uses' =>'\App\Http\Controllers\MapController@viewMap']);

The function takes no parameters and returns a view:

return view('map')->with(['mapData' => $mapData]);

I now have a need to send parameters to this controller method to get a modified view, and I'm wondering what options exist.

Is it possible to send parameters to a controller function from a Blade view without using Ajax POST? The issue with Ajax being that a response is necessary and the controller can't redirect to a view with data (or can it?).

Can I return a view with response data after Ajax POST success?

Something like: window.location = "/map"; //somehow with data, like above?

My current, very kludgy method, is to send data tied to User session to a DB table, look for that data in the controller method, and, if found, modify the view data appropriately.

Thank you!

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

There are a few options for passing data to a controller method to modify the view:

  1. Query Parameters: You can pass parameters to the controller method via the URL query string. For example, if you want to pass a parameter called "filter" with a value of "recent", you can modify your route like this:
Route::get('/viewMap/{filter}', ['uses' => '\App\Http\Controllers\MapController@viewMap']);

And in your controller method, you can access the "filter" parameter like this:

public function viewMap($filter)
{
    // Use $filter to modify the view data
    return view('map')->with(['mapData' => $mapData]);
}
  1. Form Data: You can also pass data to the controller method via a form submission. For example, you can create a form with a dropdown menu to select a filter:
<form method="POST" action="/viewMap">
    @csrf
    <select name="filter">
        <option value="recent">Recent</option>
        <option value="popular">Popular</option>
    </select>
    <button type="submit">Apply Filter</button>
</form>

And in your controller method, you can access the "filter" parameter like this:

public function viewMap(Request $request)
{
    $filter = $request->input('filter');
    // Use $filter to modify the view data
    return view('map')->with(['mapData' => $mapData]);
}
  1. Session Data: If you need to persist data across multiple requests, you can store it in the session. For example, you can store the selected filter in the session like this:
public function applyFilter(Request $request)
{
    $filter = $request->input('filter');
    session(['filter' => $filter]);
    return redirect('/viewMap');
}

And in your controller method, you can access the "filter" parameter from the session like this:

public function viewMap()
{
    $filter = session('filter');
    // Use $filter to modify the view data
    return view('map')->with(['mapData' => $mapData]);
}
djxprice's avatar

That last suggestions was spot on, good bot!

Please or to participate in this conversation.