M
M
money32232018-12-24 15:34:21
Laravel
money3223, 2018-12-24 15:34:21

How to implement an individual page for each user in Laravel?

Please tell me how to implement using a simple example of a task book, so that after registration and authorization, for each authorized user there would be individual tasks. Ivan (his notes) Vasily (his notes). I will be very grateful. CRUD implemented, but I would like to make adjustments to the project)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2018-12-25
@jazzus

If you don’t delve into the relationship yet, then there is the simplest way:
1) Create a user_id column in the tasks table. It is desirable to connect it with users, but at the initial stages it is better not to bother with such trifles)) Anyway, then 10 times then redo everything, demolish tables, rename everything, rewrite, etc.
2) When you create a task write to user_id =Auth::id(); .

$task=new Task;
$task->user_id = Auth::id();
$task->save();

Why is this? To get the id of the currently authorized user and thereby indicate that the type of task belongs to him.
In this case, you must not forget to include the auth facade ( use Auth in the controller header above the class name);
3) All task is tied. Now you need to get them.
$tasks=Task::where('user_id',Auth::id())
                                      ->get();

now the $tasks variable will contain the tasks of the currently authorized user.
4) You will probably want to sort them, count the number and make pagination.
Therefore, it will be possible to write
$tasks=Task::where('user_id',Auth::id());
$count = $tasks->count();
$tasks=$tasks->orderBy('updated_at', 'desc')
                      ->paginate(20);

These are user tasks sorted by date modified, number, and 20 entries per page.
Laravel will do pagination automatically. You will only need to place in the template.
{{ $tasks->links() }}
Everything can be run in production

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question