Larvel+layui创建评分

Larvel+layui创建评分

 

1、引入layui相关配置以及控件

2、创建一张video_score表当作user表与video表的中间表

Schema::create('video_score', function (Blueprint $table) {

            $table->increments('id');

            $table->unsignedInteger('user_id')->index();

            $table->unsignedInteger('video_id')->index();

            $table->float('score')->default(0);

            $table->timestamps();

        });

3、video.php建立模型

public function score()

    {

        return $this->belongsToMany(\App\Video::class,'video_score')->withPivot('score')->withTimestamps();

}

4、user.php建立模型

function score()

{

return $this->belongsToMany(\App\User::class,'video_score')->withTimestamps();

}

5、判断存入

$user = Auth::user();

        if(empty($user->score()->wherePivot('video_id',$video_id)->first())){

            $user->score()->attach($user->id,['score'  => $score,'video_id'=> $video_id]);

            return "ok";

        }

6、根据平均分获取数据

$videos = Video::with('post')->with('score')->withCount('score')->get();

        foreach ($videos as $video) {

           $video->scoreAvg = $video->score()->avg('score');

        }

        $data['score']=$videos->sortByDesc('scoreAvg')->take(7);

日记本

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

赞赏支持
被以下专题收入,发现更多相似内容