Retrieving saved items from the database
Let's now work on retrieving the saved items from the database and displaying them in the frontend. To begin with, we'll add a new saved
property to the metadata we put in the document head. This will be an empty array if the user is logged out, or the array of saved listing IDs associated with that user, if they're logged in.
app/Http/Controllers/ListingController.php
:
private function add_meta_data($collection, $request) { return $collection->merge([ 'path' => $request->getPathInfo(), 'auth' => Auth::check(), 'saved' => Auth::check() ? Auth::user()->saved : [] ]); }
Back in the frontend, we'll put the logic for retrieving the saved items in the beforeEach
router navigation guard. The reason we put it here and not in the addData
mutation is that we don't want to directly assign the data to the store state, but instead call the toggleSaved
mutation for each of the listings. You can't commit a mutation from another...