【发布时间】:2022-03-10 12:43:49
【问题描述】:
我有两张桌子:
posts表:
id | user_id | message
----------------------
1 | 24 | hello
2 | 96 | world
...
posts_seen表:
id | user_id | post_id
----------------------
1 | 24 | 2
2 | 96 | 1
第二个表posts_seen 仅跟踪用户“看到”(或阅读)了哪些帖子。例如,用户 #24 看过帖子 #2。
在我的Post.php 模型中,我有这个:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $table = 'posts';
public function seen()
{
return $this->hasOne('App\Models\PostSeen');
}
}
但是,当我查询帖子时,我想排除用户已经看过的帖子。现在,我只有这个:
$posts = Post::with('seen')
->get();
我想对此进行修改,如果我是用户 #24,则查询不应返回帖子 #2,因为我已经看过它。
我该怎么做?
【问题讨论】:
标签: laravel laravel-5 eloquent laravel-5.2