Skip to content
Advertisement

How can I show items belonging to a logged in user in Laravel?

I want to show the emails that the logged in user has sent from the database

This is the route code:

JavaScript

This is the function in the controller:

JavaScript

This is the a link where the user click to open the page:

JavaScript

And here where i want to show the data (i am showing only the name for test):

JavaScript

Advertisement

Answer

I think the best way to accomplish your goals here would be using a hasMany relationship between User and Emails (if emails is a Model).

JavaScript

In the controller, apply the Auth middleware to the myEmailsShow method in a constructor:

JavaScript

Then, in your myEmailsShow method, do something like the following:

JavaScript

You can remove the ID parameter from the route and just make it something like Route::get('/myEmails', 'PagesController@myEmailsShow');. Only users who are logged in will be able to access this page, and they will only see emails belonging to them.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement