在11-8 使用scope等完成专题详情页逻辑 这节课时出错
来源:11-8 使用scope等完成专题详情页逻辑

慕沐3053333
2018-05-16
(1/1)
FatalErrorException
Call to a member function getRelationExistenceCountQuery() on null
in QueriesRelationships.php (line 208)
TopicController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Topic;
class TopicController extends Controller
{
public function show(Topic $topic)
{
//带文章数的专题
$topic = Topic::withCount('postTopics')->find($topic->id);
//专题的文章列表,按照创建时间倒叙排列,前10个
$posts = $topic->posts()->orderBy('created_at','desc')->take(10)->get();
return view('topic/show',compact('topic','posts'));
}
public function submit(Topic $topic)
{
return;
}
}
Post.php
<?php
namespace App;
use App\Model;
use Laravel\Scout\Searchable;
//表=>posts
class Post extends Model
{
use Searchable;
//定义索引里的type
public function searchableAs()
{
return "post";
}
//定义有哪些字段需要搜索
public function toSearchableArray()
{
return [
'title' => $this->title,
'content' => $this->content,
];
}
//关联用户
public function user()
{
return $this->belongsTo('App\User');
}
//评论模型
public function comments()
{
return $this->hasMany('App\Comment')->orderBy('created_at', 'desc');
}
//和用户进行关联
public function zan($user_id)
{
return $this->hasOne(\App\Zan::class)->where('user_id', $user_id);
}
//文章的所有赞
public function zans()
{
return $this->hasMany(\App\Zan::class);
}
//属于某个作者的文章
public function scopeAuthorBy(Builder $query, $user_id)
{
return $query->where('user_id',$user_id);
}
public function postTopics()
{
return $this->hasMany(\App\PostTopic::class,'post_id','id');
}
//不属于某个专题的文章
public function scopeTopicNotBy(Builder $query,$topic_id)
{
return $query->doesntHave('postTopics','and',function($q) use($topic_id){
$q->where('topic_id',$topic_id);
});
}
}
Topic.php
<?php
namespace App;
use App\Model;
use Laravel\Scout\Searchable;
//表=>posts
class Post extends Model
{
use Searchable;
//定义索引里的type
public function searchableAs()
{
return "post";
}
//定义有哪些字段需要搜索
public function toSearchableArray()
{
return [
'title' => $this->title,
'content' => $this->content,
];
}
//关联用户
public function user()
{
return $this->belongsTo('App\User');
}
//评论模型
public function comments()
{
return $this->hasMany('App\Comment')->orderBy('created_at', 'desc');
}
//和用户进行关联
public function zan($user_id)
{
return $this->hasOne(\App\Zan::class)->where('user_id', $user_id);
}
//文章的所有赞
public function zans()
{
return $this->hasMany(\App\Zan::class);
}
//属于某个作者的文章
public function scopeAuthorBy(Builder $query, $user_id)
{
return $query->where('user_id',$user_id);
}
public function postTopics()
{
return $this->hasMany(\App\PostTopic::class,'post_id','id');
}
//不属于某个专题的文章
public function scopeTopicNotBy(Builder $query,$topic_id)
{
return $query->doesntHave('postTopics','and',function($q) use($topic_id){
$q->where('topic_id',$topic_id);
});
}
}
3回答
-
慕少7438843
2019-08-25
解决了吗?能告诉一下解决方法吗
00 -
ziruiding
2019-04-12
请问您有解决方法了吗 求分享
00 -
轩脉刃
2018-05-17
FatalErrorException
Call to a member function getRelationExistenceCountQuery() on null
in QueriesRelationships.php (line 208)
错误信息能不能再给详细点?
022019-04-15
相似问题