Laravel 5.8 做个知乎 14 —— 用户关注

1 创建表

php artisan make:migration create_followers_table --create=followers

 

    public function up()
    {
        Schema::create(‘followers‘, function (Blueprint $table) {
            $table->bigIncrements(‘id‘);
            //关注者id
            $table->integer(‘follower_id‘)->unsigned()->index();
            //被关注者id
            $table->integer(‘followed_id‘)->unsigned()->index();
            $table->timestamps();
        });
    }

 

php artisan migrate

2 代码

2.1 \resources\views\questions\show.blade.php

Laravel 5.8 做个知乎 14 —— 用户关注
 <div class="card">
                    <div class="card-header follow">
                        <h5>关注作者</h5>

                    </div>
                    <div class="card-body">
                        <div class="media">
                            <div class="media-left">
                                <a href="#">
                                    <img width="36px;" src="{{$question->user->avatar}}" alt="{{$question->user->name}}">
                                </a>
                            </div>
                        </div>
                        <div class="media-body">
                            <h4 class="media-heading">
                                <a href="">{{$question->user->name}}</a>
                            </h4>
                        </div>
                        <div>
                            <span>问题</span>
                            <span>{{$question->user->questions_count}}</span>
                            <span>答案</span>
                            <span>{{$question->user->answers_count}}</span>
                            <span>评论</span>
                            <span>{{$question->user->comments_count}}</span>
                        </div>
                        <!-- vue 组件 -->
                        <user-follow-button user="{{$question->user_id}}"></user-follow-button>
                        <!-- vue 组件 -->
                        <send-message user="{{$question->user_id}}"></send-message>
                    </div>
                </div>
View Code

2.2  app\User.php

//关注用户
    public function followersUser()
    {
        //1 类 2 表名 3 外键 4 外键
        return $this->belongsToMany(self::class, ‘followers‘, ‘followed_id‘, ‘follower_id‘)->withTimestamps();
    }

 

Laravel 5.8 做个知乎 14 —— 用户关注

上一篇:mysql常用语句


下一篇:Java学习笔记day17-泛型-Set