Ghanem rating的使用
//进入haxibiao.com里
cd /data/www/haxibiao.com
//通过composer拉入包
composer require ghanem/rating
//进入app/config/app.php添加下面
' providers ' => [
Ghanem \ Rating \ RatingServiceProvider :: class ];
//生成迁移
php artisan rating:migration
//迁移后会多出一张ratings的表
php artisan migrate
//在video模型里面
use Ghanem\Rating\Traits\Ratingable as Rating;
class Video extends Model{
use Rating;
}
//然后就可以对video进行评分,数据需从页面传递
$user = Auth::user();
$video = Video::find($video_id);
$rating = $video -> ratingUnique(['rating' =>$score],$user);
//判断是否已经评分,先添加Rating模型
php artisan make:model Rating
//在Rating模型里面
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Rating extends Model
{
protected $table = 'ratings';
protected $fillable = [
'rating',
'ratingable_type',
'ratingable_id',
'author_type',
'author_id'
];
}
//在user模型里面对Rating一对多关联
public function ratings(){
return $this->hasMany('App\Rating','author_id','id');
}
//判断
if(empty($user->ratings()->where('ratingable_id',$video_id)
->where('ratingable_type','App\Video')
->first()))
这个人暂时没有 freestyle