这个问题能给出正确的解答吗,问题区好几个人问,都没给出解决的办法

这个问题能给出正确的解答吗,问题区好几个人问,都没给出解决的办法

<?php


namespace app\index\controller;


use app\index\model\Tag as TagModel;

use app\index\model\Topic as TopicModel;

use app\index\model\TopicTag as TopicTagModel;

use think\Controller;


class Topic extends Controller {

    public function newTopic() {

        if (request()->isPost()) {

            $postData = input('post.');

            $topic = new TopicModel();

            $user = session('user');

            $topic->title = $postData['title'];

            $topic->category_id = $postData['category_id'];

            $topic->content = $postData['content'];

            $topic->user_id = $user->id;

            $topic->created_at = intval(microtime(true));

            $topic->save();

            //处理标签

            $tags = $postData['tags'];

            foreach ($tags as $tag) {

                if (is_numeric($tag)) {

                    $this->createTopicTag($tag, $topic->id);

                    continue;

                }

                $newTag = $this->createTag($tag);

                $this->createTopicTag($newTag->id, $topic->id);

            }

            $this->success('恭喜!帖子创建成功!');

        }


        $this->assign([

            'user' => session('user'),

            'category' => config('category'),

            'tags' => TagModel::all()

        ]);

        echo $this->fetch('new_topic');

    }


    private function createTopicTag($tagId, $topicId) {

        $topicTag = new TopicTagModel();

        $topicTag->tag_id = $tagId;

        $topicTag->topic_id = $topicId;

        $topicTag->save();

    }


    private function createTag($name) {

        $tag = new TagModel();

        $tag->name = $name;

        $tag->save();

        return $tag;

    }


    public function detail() {

        $topicId = input('get.id');

        $topic = TopicModel::getTopic($topicId);

        $user = session('user');

        $this->assign([

            'user' => $user,

            'topic' => $topic,

            'topicTags' => TopicTagModel::getTopicTagsByTopicId($topic->id),

            'categoryNames' => getCategoryNames($topic->category_id),

        ]);

        echo $this->fetch('detail');

    }

}

common.php文件

<?php


// 应用公共文件

function getCategoryNames($id) {


    $category =[

        'imooc' => [

            1 => '站务与公告',

            2 => '反馈',

            3 => '使用指南',

        ],

        'Mobile' => [

            8 => 'Android',

        ],

        'Lifestyle' => [

            10 => '意欲蔓延',

        ],

        'Technology' => [

            5 => '程序猿',

            6 => '分享与创造',

        ],

    ];


    //$category = config('category');

    foreach($category as $key => $cate) {

        foreach($cate as $category_id => $categoryName) {

            if ($category_id == $id) {

                return [$key, $categoryName];

            }

        }

    }

    return [];

}


model文件夹下的Topic.php

<?php


namespace app\index\model;



use think\Model;


class Topic extends Model{

    public static function getTopic($id) {

        return self::find(['id' => $id]);

    }


    public function user() {

        return $this->belongsTo('User', 'user_id');

    }


    /*public static function getTopic($id) {

        return self::withCount(['replies', 'praises'])-find($id);

    }*/

}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
guly 2018-07-18 18:01:35

你好,提示不存在replies()方法

建议添加以下代码到 model\topic.php文件中

public function replies() {
  return $this->hasMany('Reply', 'topic_id');
}

同时建议同学下载老师课堂源码进行调试学习,祝学习愉快!

  • 提问者 时光一树 #1
    真的是被这个老师打败了,代码看他视屏一个一个敲的
    2018-07-18 18:04:31
  • guly 回复 提问者 时光一树 #2
    如果对老师敲代码演示有误建议下载源码,在源码的基础上跟着老师的节奏学习,如果有做的不好的地方请见谅,祝学习愉快!
    2018-07-18 18:15:55
guly 2018-07-18 15:38:41

你好,建议参考一下newTopic()和 model下getTopic方法尝试:

public function newTopic(){
  if (request()->isPost()) {
    $postData = input('post.');
    $user = session('user');
    $topicId = input('get.topicId');
    if (!$topicId) {
      $topic = new TopicModel();
    } else {
      $topic = TopicModel::find($topicId);
    }
    $topic->title = $postData['title'];
    $topic->category_id = $postData['category_id'];
    $topic->content = $postData['content'];
    $topic->user_id = $user->id;
    $topic->created_at = intval(microtime(true));
    $topic->save();
    // 标签处理
    $tags = $postData['tags'];
    TopicTagModel::where(['topic_id' => $topic->id])->delete();
    foreach($tags as $tag) {
      if (is_numeric($tag)) {
        $this->createTopicTag($tag, $topic->id);
        continue;
      }
      $newTag = $this->createTag($tag);
      $this->createTopicTag($newTag->id, $topic->id);
    }
    $message = $topicId ? '编辑帖子成功!' : '发表帖子成功!';
    return $this->success($message, 'topic/index');
  }
  $tags = TagModel::all();
  $this->assign([
    'user' => session('user'),
    'category' => config('category'),
    'tags' => $tags
  ]);
  echo $this->fetch('new_topic');
}
public static function getTopic($id) {
  return self::withCount(['replies', 'praises'])->find($id);
}

如果解决您的问题请采纳,祝学习愉快!

  • 提问者 时光一树 #1
    不行,提示这个错误:method not exist:think\db\Query->replies,哎,搞两天了,一直在查百度查问题区,这个问题解决不了,剩下的视屏根本没法看
    2018-07-18 17:25:01
提问者 时光一树 2018-07-18 13:57:12

类的属性不存在:app\index\model\Topic->praises_count

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师