0
votes

I have a shop-store project. This project starts with basic functionality and grow up on each software life-cycle. Recently tendency to deploy some customization for user and analytics grow. I wanted to add favorite capability to project. The project has guest mode which unauthenticated user can see products.

Each user may choose to add a product to his/her favorites(after Auth). so far so good. I decide to append favorite attribute to products. Trouble comes when i try to return products from controller as response. If i use Auth middleware unauthenticated users redirected to login. If i use custom code, every API with product should be changed accordingly. If i try to append filed in product model, i don't have access to Auth class nor requestin product model. What's the way to handle such a situation? I appreciate your help.

UPDATE: There are many models in my project. Order, OrderProduct, Product, User, Payment, Category, Delivery, .... I want to have a new table named Bookmark which has two column: user_id and product_id.

Product == One Many == Bookmark

User == One Many == Bookmark

1
Please provide where you have been stuck exactly. Sometimes the problem description is not properly brief your current problem. - Hashemi Rafsan
I want to append favorites for every product. User must've be authenticated for this purpose. Also guest(Unauthenticated) users have same api/controller. I can't rewrite all controllers which returns products in order to differentiate between authenticated and unauthenticated users - Mehran Zamani
How many models do you have and what is the relationship between them? - David Enoma
It'd be helpful if you post some code - David Enoma
@DavidEnoma return Product::find($id)->first(); it returns a row of products table. If user is authenticated it should append bookmark attribute. - Mehran Zamani

1 Answers

0
votes

You can use the blade @auth directive to check for user authentication.

Then for templating

{{auth()-> user -> bookmark -> all()}}

This should work if you have established a one to may relationship between user and bookmark in your model.