Java教程

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

本文主要是介绍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

 <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 —— 用户关注的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!