skip to Main Content

I have a web application TODO list. Users can add, modify, and delete tasks. Issue is that every user who log in to my web application can see all tasks that are created by other users. My idea is to add cell to tasks database table with user_id (ID of the user that created task), then somehow application get this user_id and print out tasks with this ID. Can you describe please how can I realise this thing?

I am working with Laravel 10 and Vue.

I tried to get this users id and put in tasks database but it didn’t work out. I tried it to do with User and Task model.

2

Answers


  1. You’ll need to associate each task with the user who created it and then filter tasks based on the logged-in user’s ID.

    1. First, ensure your tasks table includes a user_id column that
      references the id of the user in your users table.
    2. Ensure your Task model is set up to belong to a User, and your User
      model can have many Tasks.
    3. When creating a task, ensure you’re saving the user_id of the currently authenticated user alongside the task.
    4. Modify your method that retrieves tasks to only return tasks for the currently authenticated user
    Login or Signup to reply.
  2. there can be multiple ways to handle this.
    you need to create a relationship between users and tasks.

     public function tasks()
    {
        return $this->hasMany(Task::class);
    }
    

    create a foreign key in tasks table (user_id) and save when you are saving the task.
    In controller function, all you need to call the values like this

    $user = User::find($id);
    $userTasks = $user->tasks();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search