请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

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

(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

3回答

慕少7438843 2019-08-25 16:35:10

解决了吗?能告诉一下解决方法吗


0 回复 有任何疑惑可以回复我~
ziruiding 2019-04-12 17:40:11

请问您有解决方法了吗 求分享

0 回复 有任何疑惑可以回复我~
轩脉刃 2018-05-17 11:11:00

FatalErrorException                                     

Call to a member function getRelationExistenceCountQuery() on null

in QueriesRelationships.php (line 208)    

错误信息能不能再给详细点?

0 回复 有任何疑惑可以回复我~
  • 遇到了同样的问题急待解决:
    (1/1) FatalThrowableError
    Call to a member function getRelationExistenceCountQuery() on null
    in QueriesRelationships.php line 208
    at Builder->withCount(array('postTopics'))
    in Model.php line 1374
    at Model->__call('withCount', array('postTopics'))
    in Model.php line 1386
    at Model::__callStatic('withCount', array('postTopics'))
    in TopicController.php line 14
    at TopicController->show(object(Topic))
    at call_user_func_array(array(object(TopicController), 'show'), array('topic' => object(Topic)))
    in Controller.php line 55
    at Controller->callAction('show', array('topic' => object(Topic)))
    in ControllerDispatcher.php line 44
    at ControllerDispatcher->dispatch(object(Route), object(TopicController), 'show')
    回复 有任何疑惑可以回复我~ 2019-04-12 17:39:48
  • 轩脉刃 回复 ziruiding #2
    像是这个topicid在数据库中不存在,就是find($topic->id)里面的这个id
    回复 有任何疑惑可以回复我~ 2019-04-15 09:58:06
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号