如何根据我的投票表中的记录更新图像的upvotes和downvotes?

时间:2018-05-26 13:23:45

标签: php laravel eloquent

我创建了upvote / downvote功能,它使用了一个名为voted的表。该表中的每条记录都包含一个id,user_id,image_id和vote,它是整数(1表示upvote,0表示downvote)。我的图像表包含2列,一列用于upvotes,一列用于downvotes。

现在的问题是,我不确定如何更新我的图像表中的upvotes / downvotes列以及何时更新它们。我认为我应该在投票后立即这样做,但我不完全确定。

任何帮助都将不胜感激。

投票模型:

class Vote extends Model
{
    public function images(){
        return $this->belongsTo('App\Images');
    }
}

图片模型:

class Image extends Model
{
    public function votes(){
        return $this->hasMany('App\Vote');
    }
}

图片表:

Schema::create('images', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('name');
            $table->string('description')->nullable()->default(null);
            $table->integer('user_id')->unsigned();
            $table->string('file_name');
            $table->string('upvotes')->default(0);
            $table->string('downvotes')->default(0);
            $table->string('views')->default(0);
            $table->timestamps();
            $table->engine = 'InnoDB';
        });

投票表:

Schema::create('votes', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->integer('user_id')->unsigned();
            $table->integer('image_id')->unsigned();
            $table->boolean('vote');
            $table->timestamps();
            $table->engine = 'InnoDB';
        });

VotesController

class VotesController extends Controller
{
    public function voteImage(Request $request){
        $image_id = $request['imageId'];
        $isLike = $request['isLike'] === 'true';
        $update = false;
        $image = Image::find($image_id);
        if (!$image){
            return null;
        }
        $user = Auth::user();
        $vote = $user->votes()->where('image_id', $image_id)->first();
        if ($vote) {
            $already_vote = $vote->vote;
            $update = true;
            if ($already_vote == $isLike){
                $vote->delete();
                return null;
            }
        } else {
            $vote = new Vote();
        }
        $vote->vote = $isLike;
        $vote->user_id = $user->id;
        $vote->image_id = $image->id;
        if ($update) {
            $vote->update();
        } else {
            $vote->save();
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:1)

你需要在投票改变之后这样做。 将方法添加到图像模型

class Image extends Model
{
    public function votes(){
        return $this->hasMany('App\Vote');
    }

    public function updateVotes()
    {
        $this->upvotes = Vote::where('image_id', $this->id)->where('vote', true)->count();
        $this->downvotes = Vote::where('image_id', $this->id)->where('vote', false)->count();
        $this->save();
    }
}

并在控制器

中使用它
class VotesController extends Controller
{
    public function voteImage(Request $request){
        $image_id = $request['imageId'];
        $isLike = $request['isLike'] === 'true';
        $update = false;
        $image = Image::find($image_id);
        if (!$image){
            return null;
        }
        $user = Auth::user();
        $vote = $user->votes()->where('image_id', $image_id)->first();
        if ($vote) {
            $already_vote = $vote->vote;
            $update = true;
            if ($already_vote == $isLike){
                $vote->delete();
                $image->updateVotes();
                return null;
            }
        } else {
            $vote = new Vote();
        }
        $vote->vote = $isLike;
        $vote->user_id = $user->id;
        $vote->image_id = $image->id;
        if ($update) {
            $vote->update();
        } else {
            $vote->save();
        }
        $image->updateVotes();
        return null;
    }
}