mysql - 如何为 'users who view my profile' 创建逻辑?

标签 mysql laravel eloquent

这可能是一个新手问题,但我不知道如何编写这个逻辑...... 假设我有一个包含用户和个人资料的应用程序,我想处理一个 View ,显示所有看到我(身份验证用户)个人资料的用户。

我该怎么做?

谢谢

最佳答案

也许是这样的?

在用户模型中创建一个记录访问的函数,每当显示用户页面时,您需要从用户 Controller 触发该函数。

public function logPageView()
{
    DB::table('user_views')->insert(array(
        'user_id' => $this->id,
        'agent' => Request::server('HTTP_USER_AGENT'),
        'user' => Auth::check()?Auth::user()->username:null,
        'session' => Session::getId(),
        'created_at' => \Carbon\Carbon::now(),
        'updated_at' => \Carbon\Carbon::now()
    ));
}

在用户 Controller 中创建一个 show_views 函数来显示记录的 View 。 groupBy 确保每天每个 session 仅显示一次访问。您可以删除或调整它以满足您的需要。

public function show_views($id)
{
    $views = DB::table('user_views')
        ->where('user_id', $id)
        ->orderBy('created_at', 'DESC')
        ->groupBy('session', DB::raw('DATE(created_at)'))
        ->get();

    return View::make('user_views.show', [
        'views' => $views,
        'id' => $id
    ]);
}

至少这应该能让你继续前进:)

关于mysql - 如何为 'users who view my profile' 创建逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28493892/

相关文章:

php - Eloquent 地同时在多个表上使用嵌套的 json 结果,如 'with' 所做的

mysql - 两个索引条目之间的行?

php - 重命名 MySQL 中的重复行

php - 按顺序对输出进行编号

mysql - Laravel 在 whereHas 中使用 select

php - 关系的 Eloquent 查询范围

c# - 查询映射的 CustomObject 时出现 NullPointerException

php - Laravel 在查找事件/ Hook 之前或之后

php - Laravel 4,检查路由参数是否存在于数据库中

laravel - 传递数组后在laravel中获取共享变量