Reputation: 5782
I've created upvote/downvote functionality which uses a table called voted. Each record in that table contains an id, user_id, image_id and vote which is integer ( 1 for upvote, 0 for downvote ). My images table contains 2 columns, one for amount of upvotes and 1 for amount of downvotes.
Now the problem is that I'm not sure how exactly to do the update of the upvotes/downvotes columns in my image table and when exactly to update them. I assume I should do that right after voting but I'm not completely sure.
Vote model:
class Vote extends Model
{
public function images(){
return $this->belongsTo('App\Images');
}
}
Image model:
class Image extends Model
{
public function votes(){
return $this->hasMany('App\Vote');
}
}
Images table:
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';
});
Votes table:
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;
}
}
Upvotes: 0
Views: 155
Reputation: 881
you need to do this after any change in the votes. add method to Image model
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();
}
}
and user it in controller
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;
}
}
Upvotes: 1