• Laravel 5.8 做个知乎 11 —— 添加关注 toggle方法 的使用


    1 创建关注表数据

    php artisan make:migration create_user_question_table --create=user_question

    databasemigrations2021_07_15_002200_create_user_question_table.php

        public function up()
        {
            Schema::create('user_question', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->integer('user_id')->unsigned()->index();
                $table->integer('question_id')->unsigned()->index();
                $table->timestamps();
            });
        }
    View Code
    php artisan migrate

    2 model

    2.1 Follow

    php artisan make:model Follow

    appFollow.php

    <?php
    
    namespace App;
    
    use IlluminateDatabaseEloquentModel;
    
    class Follow extends Model
    {
        //
        protected $table = 'user_question';
        protected $fillable = ['user_id','question_id'];
    }
    View Code

    2.2 User

    appUser.php

       /* public function follows($question)
        {
            return Follow::create([
                'question_id'   =>$question,
                'user_id'       =>$this->id
            ]);
        }*/
       public function follows()
       {
           return $this->belongsToMany(Question::class,'user_question')
             ->withTimestamps();
       }
       public function followThis($question)
       {
           return $this->follows()->toggle($question);
       }
        
        public function followed($question)
        {
            return !! $this->follows()->where('question_id',$question)->count();
        }
    View Code

    2.3 Question

    appQuestion.php

        public function followers()
        {
            return $this->belongsToMany(User::class,'user_question')
              ->withTimestamps();
        }
    View Code

    3 控制器

    php artisan make:controller QuestionFollowController
    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    use IlluminateSupportFacadesAuth;
    
    class QuestionFollowController extends Controller
    {
        public function __construct()
        {
            $this->middleware('auth');
        }
        
        
        //
        public function follow($question)
        {
            Auth::user()->followThis($question);
            return back();
        }
    }
    View Code

    4 路由

    web.php

    Route::get('questions/{question}/follow','QuestionFollowController@follow');

    5 模板

    esourcesviewsquestionsshow.blade.php

                <div class="col-md-3">
                    <div class="card">
                        <div class="card-header">
                            <h2>{{ $question->followers_count }}</h2>
                            <span>关注者</span>
                        </div>
                        <div class="card-body">
    
                            <a href="/questions/{{$question->id}}/follow"
                               class="btn  {{Auth::check() &&  Auth::user()->followed($question->id) ? 'btn-success':'btn-info' }}">
                                {{Auth::check() && Auth::user()->followed($question->id) ?'已关注' :'关注该问题'     }}
    
                            </a>
                            <a href="#container" class="btn btn-primary">
                                撰写答案
                            </a>
                        </div>
                    </div>
                </div>
  • 相关阅读:
    HashMap_经典存储_经典分拣思路
    (GOF23设计模式)_单例模式_应用场景_饿汉式_懒汉式
    sql简单测试
    Java概述
    搭建集群时的问题总结
    在Java连接hbase时出现的问题
    JavaAPI与hbase的交互
    Resourcemanager的高可用
    hbase安装
    hbase报错org.apache.hadoop.hbase.TableExistsException: hbase:namespace
  • 原文地址:https://www.cnblogs.com/polax/p/15017528.html
Copyright © 2020-2023  润新知