专题中获取属于专题文章和不属于专题文章出错
来源:11-6 使用scope实现某个用户未投稿的文章

thinkphp
2019-03-14
Post模型中的关联
//属于某一个作者文章
public function scopeAuthorBy(Builder $query,$user_id)
{
return $query->where('user_id',$user_id);
}
//文章拥有多少个专题
public function postTopics()
{
return $this->hasMany(\App\Topic,'post_id','id');
}
//不属于某个专题的文章
public function scopeTopicNotBy(Builder $query,$topic_id)
{
//外边是不在
return $query->doesntHave('postTopics','and',function($q) use($topic_id){
//函数内就要判断外边判断的topic_id在文章拥有的专题中
$q->where('topic_id',$topic_id);
});
}
TopicController控制器代码
//带文章数的专题
$topic = Topic::withCount('postTopics')->find($topic->id);
//专题的文章列表,倒序,前十个
$posts = $topic->posts()->orderBy('created_at','desc')->take(10)->get();
//属于我的文章但不属于这个专题,可以投稿的文章
//使用scopeAuthorBy获取 属于用户的文章 ;使用scopeTopicNotBy不属于专题的文章
$myposts = \App\Post::authorBy(\Auth::id())->topicNotBy($topic->id)->get();
return view('topic.show',compact('topic','posts','myposts'));
执行的时候出错
请问下是为什么?该怎么解决呢?
1回答
-
thinkphp
提问者
2019-03-17
use Illuminate\Database\Eloquent\Builder;
添加这个就可以了
00
相似问题