如何获取Laravel中每本书的最新章节URL?

如何获取Laravel中每本书的最新章节URL?

问题描述:

i have a book site built with laravel, is there any way to know and store lastest chapter URL visited of each book on database? For help next times, when user come a book, he will know where he can countinue reading. Thanks!

我有一个用laravel构建的书籍网站,有没有办法知道和存储每本书访问过的最新章节URL 在数据库? 下次帮助时,当用户拿到书时,他会知道他在哪里可以统计阅读。 谢谢! p> div>

Using cookies will allow the user to pick up where they left off, even if they are not logged in, but you should set it on the user account as well, that way you can track user progress even if they sign in on a different device. It helps if users have to log in to read your book, because then you'll always have an authenticated user you can register the log to.

I would suggest you track user ID, chapter ID and perhaps book ID, plus a datetime (can be a created_at column that is populated automatically). By logging these things you can tell a lot more about book usage and user behavior than if you just log what URL they visited last.

You can listen for the retreived event and then create the log item:

<?php

class Chapter extends Model
{
    protected $dispatchesEvents = [
        'retreived' => LogUserReading::class,
    ];
}

class LogUserReading
{
    public function handle(Chapter $chapter)
    {
        ReadingLog::create(Auth::user()->id, $chapter->id, $chapter->book->id);
    }
} 

Then to find out for instance where a user last left off, just fetch the latest item from the reading log.

Try using Redis - https://laravel.com/docs/5.6/redis

 Redis::set('latestChapter', '35');