【问题标题】:How to update image's upvotes and downvotes based on the records in my votes table?如何根据我的投票表中的记录更新图像的赞成票和反对票?
【发布时间】:2018-11-05 16:21:51
【问题描述】:

我创建了 upvote/downvote 功能,它使用了一个名为 voted 的表。该表中的每条记录都包含一个 id、user_id、image_id 和整数投票(1 表示赞成,0 表示反对)。我的图像表包含 2 列,一列表示赞成票数,一列表示反对票数。

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

任何帮助将不胜感激。

投票模型:

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';
        });

投票控制器

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;
    }
}

【问题讨论】:

  • 您是为了解决方案还是代码审查?

标签: php laravel eloquent


【解决方案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;
    }
}

【讨论】:

  • 完美运行,只需几行代码。我设计的方式需要大量重复的代码。
猜你喜欢
  • 2018-07-08
  • 1970-01-01
  • 2013-10-24
  • 2021-06-19
  • 1970-01-01
  • 2011-05-19
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多