laravel投票系统,使用poltmorphic mongodb

时间:2019-09-10 04:18:33

标签: laravel mongodb polymorphism voting

我需要在我的项目中创建投票系统时遇到一些问题,我想要投票Post,1票获得20分。 1票= 20分

如何创建投票功能。

这是我的投票模式

protected $connection = 'mongodb';
protected $collection = 'votes';
protected $dates = ['deleted_at'];
protected $fillable = ['user_id', 
                       'vote_id',
                       'vote_type',
                       'point'];

//slug
public function sluggable()
{
    return [
        'slug' => [
            'source' => 'title',
            'onUpdate' => true
        ]
    ];
}
public function getRouteName() 
{
    return 'slug';
}
public function user(){
    return $this->belongsTo('App\user');
}
public function vote(){
    return $this->morphTo();
}

这是我的帖子模型:

protected $connection = 'mongodb';
protected $collection = 'posts';
protected $dates = ['deleted_at'];
protected $fillable = ['user_id','title','description','image'];
// slug
public function sluggable()
{
    return [
        'slug' => [
            'source' => 'title',
            'onUpdate' => true
        ]
    ];
}
public function getRouteName() 
{
    return 'slug';
}
public function user() {
    return $this->belongsTo('App\User');
}

public function vote() {
    return $this->morphMany('App\Models\Vote', 'voteable');
}

这是我在PostController中的表决功能:

public function vote($id) 
{
    $vote = Auth::user()->find($id);
    $vote->save();
    return redirect()->back();

}

请咨询 非常感谢

0 个答案:

没有答案
相关问题