0

Im working on a project to learn Vue.js with Laravel. I created a simple tasks application, where its possible to create, modify, mark as done, and delete tasks.

I wish to make it user individual, so i ran artisan make:auth to create the auth template. Added column : user_id to tasks table. In the Index method of TaskController instead of - return Task::all(); I tried to do - return Task::where('user_id', Auth::id() ); but its not working as I wish... If instead of Auth::id() I put 1 for example, it does return the tasks for user id 1.

Can anyone help me out?

2
  • Are you logged in? Commented Jan 4, 2018 at 23:49
  • yes, i`m logged in. Also tried to re-login, still nothing. Commented Jan 5, 2018 at 20:34

1 Answer 1

1

Check in your code that Auth::id() is actually returning the user's ID - it's likely that your user isn't authenticated and is therefore returning null, so the query doesn't work as you expect.

Also, if you set up your associations correctly (that is, a user hasMany tasks) then you could simplify the code by using the association instead.

return Auth::user()->tasks;

// Achieves same result as...

return Task::where('user_id', Auth::id())->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.