archvist
archvist

Reputation: 722

Laravel 5.2 - Returning two controllers to the same view

I have a page controller which simply returns a view with all of my users e.g. -

public function index()
{
  $users = DB::table('users')->select('id', 'account_id', 'email', 'name')->paginate(10);

  return view('admin/home')->with('users', $users);
}

On that page, I have a search input which I want to return to the same view with the same variable data -

public function searchUser(Request $request)
{
  $searchTerm = $request->input('search');

  $search = '%'.$searchTerm.'%';

  $users = User::where('name', 'LIKE', $search)
                ->orwhere('email', 'LIKE', $search)
                ->orwhere('account_id', 'LIKE', $search)
                ->get();

  return redirect()->route('admin.home')->with('users', $users);
}

The problem is when I search and use the SearchUser function it also runs the index() function which returns all the users. I'm assuming I need to append something to the url so it would have something like

/site?searchterm

Although I'm not sure how that would work.

I understand with a get request you can pass in an optional parameter but my search form is a post type.

Upvotes: 1

Views: 350

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

In searchUser() return the same view:

return view('admin.home')->with('users', $users);

Instead of redirecting:

return redirect()->route('admin.home')->with('users', $users);

Upvotes: 2

Related Questions