Search code examples
javascriptajaxlaravelget

AJAX request to Laravel resource controller, best practice


I have set up a resource controller in Laravel with the following index function:

public function index()
{
    if (!Auth::check()) {
        return redirect()->route('login');
    }
    $decks = Auth::user()->decks->sortByDesc('name');
    return view('decks.index')->with('decks', $decks);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
}

On a different page I also need the $decks variable via a AJAX call. Right now I have set up an additional route to my controller, from which I can retrieve the decks via a GET request:

public function getDecks()
{
    if (!Auth::check()) {
        return;
    }
    $decks = Auth::user()->decks->sortByDesc('name');
    return response()->json($decks);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
}

My question: Is there a way to get the $decks variable via a request directly to index or is my solution the way to go?

If I make a get request to index I get the HTML of the decks.index view, but how can I access (if possible) the $decks variable?

I guess what I don't really grasp is this: What happens to $decks in the ->with('decks', $decks) statement? I know I can then access $decks using blade syntax on that page, but from where does it access the data and can I also access it via AJAX?


Solution

  • You can not retrieve the $decks from the index route directly. When you call view(..)->with(...) it is internally passed to the blade template processor which also receives the $decks variable, the HTML is then build on the server-side and the 'compiled' HTMl is then returned to the browser. So once the server returns the result the $decks variable is not present anymore. Because of this behavior it is not possible to do what you want.

    So yes, your solution is the way to go, although you might consider wrapping the Auth::check() in a middleware and move the call to $decks to a separate function in order to simplify your code.

    I hope this answers your question, if anything is unclear feel free to ask!