Laravel 尝试检查用户是否与 post 有关系
Laravel trying to check if the user has relationship with post
我有posts,这些posts可以被用户保存以供日后阅读。我创建了这个关系,我可以很容易地保存或删除它们。问题是我无法检查 post 是否保存在前端。现在我写了一些代码来处理这个问题,但它似乎不起作用。这是我的控制器代码:
$articleFlag = 1;
$userID = Auth::User()->id;
if (count($bestarticles) > 0) {
foreach ($bestarticles as $bestarticle) {
$saveddata = DB::table('savearticle')->where('user_id', $userID && 'article_id', $bestarticle);
if (count($saveddata) > 0) {
$articleFlag = 1;
} else {
$articleFlag = 2;
}
} //foeach endes here
} //first if endes here
然后我将 $articleFlag
传递给视图而不是用 if
检查它的值
但问题是,无论我做什么 if (count($bestarticles) > 0)
returns true ,我都会看到值 1。
有人知道我可能遗漏了什么吗?
这是我的用户控制器关系:
function savedarticle(){
return $this->belongsToMany('App\User', 'savearticle', 'user_id',
'article_id');
}
下面是我用于保存和删除的函数:
function savethearticle(Article $article){
$this->savedarticle()->syncWithoutDetaching([$article->id]);
}
function removethearticle(Article $article){
$this->savedarticle()->detach([$article->id]);
}
但您无需担心。我可以删除和添加。
或者是否有另一种方法来检查视图中的现有关系,或者有更好的方法在控制器中检查它并传递到视图中?
我正在使用 Laravel 5.4。
你不应该在 Where 子句中传递 bestarticle 的 ID 吗?此外,它需要一个 ->get() 来实际触发对数据库的请求和 运行 查询。
$saveddata = DB::table('savearticle')->where('user_id', $userID && 'article_id', $bestarticle);
应该是
$saveddata = DB::table('savearticle')->where('user_id', $userID && 'article_id', $bestarticle->id)->get();
您似乎有 Collection
个 Article
个模型,您正在尝试确定它是否与 User
相关。
如果是这样,我建议您在最初查询 Article
模型时预先加载 User
关系。这样做的好处是使用一个查询来加载关系,而不是每个 Article
.
$userId = Auth::id();
$articles = Article::with(['savedarticle' => function ($query) use ($userId) {
return $query->where('user_id' => $userId);
}])->get();
有了这个 Collection
,因为我们已经专门加载了当前经过身份验证的 User
,您可以继续知道如果 savedarticle
关系具有 count
1
,即 User
关系存在。
foreach ($articles as $article) {
if ($article->savedarticle->count()) {
// User has already saved article
} else {
// User has not saved article
}
}
我有posts,这些posts可以被用户保存以供日后阅读。我创建了这个关系,我可以很容易地保存或删除它们。问题是我无法检查 post 是否保存在前端。现在我写了一些代码来处理这个问题,但它似乎不起作用。这是我的控制器代码:
$articleFlag = 1;
$userID = Auth::User()->id;
if (count($bestarticles) > 0) {
foreach ($bestarticles as $bestarticle) {
$saveddata = DB::table('savearticle')->where('user_id', $userID && 'article_id', $bestarticle);
if (count($saveddata) > 0) {
$articleFlag = 1;
} else {
$articleFlag = 2;
}
} //foeach endes here
} //first if endes here
然后我将 $articleFlag
传递给视图而不是用 if
检查它的值
但问题是,无论我做什么 if (count($bestarticles) > 0)
returns true ,我都会看到值 1。
有人知道我可能遗漏了什么吗?
这是我的用户控制器关系:
function savedarticle(){
return $this->belongsToMany('App\User', 'savearticle', 'user_id',
'article_id');
}
下面是我用于保存和删除的函数:
function savethearticle(Article $article){
$this->savedarticle()->syncWithoutDetaching([$article->id]);
}
function removethearticle(Article $article){
$this->savedarticle()->detach([$article->id]);
}
但您无需担心。我可以删除和添加。
或者是否有另一种方法来检查视图中的现有关系,或者有更好的方法在控制器中检查它并传递到视图中?
我正在使用 Laravel 5.4。
你不应该在 Where 子句中传递 bestarticle 的 ID 吗?此外,它需要一个 ->get() 来实际触发对数据库的请求和 运行 查询。
$saveddata = DB::table('savearticle')->where('user_id', $userID && 'article_id', $bestarticle);
应该是
$saveddata = DB::table('savearticle')->where('user_id', $userID && 'article_id', $bestarticle->id)->get();
您似乎有 Collection
个 Article
个模型,您正在尝试确定它是否与 User
相关。
如果是这样,我建议您在最初查询 Article
模型时预先加载 User
关系。这样做的好处是使用一个查询来加载关系,而不是每个 Article
.
$userId = Auth::id();
$articles = Article::with(['savedarticle' => function ($query) use ($userId) {
return $query->where('user_id' => $userId);
}])->get();
有了这个 Collection
,因为我们已经专门加载了当前经过身份验证的 User
,您可以继续知道如果 savedarticle
关系具有 count
1
,即 User
关系存在。
foreach ($articles as $article) {
if ($article->savedarticle->count()) {
// User has already saved article
} else {
// User has not saved article
}
}