0

I have a function in controller like this-

function add_votes()
{
    $input = Request::all();
    $check_if_exists = DB::table('webinar_vote')
        ->where('webinar_id', '=', $input['uuid'])
        ->first();
    if (is_null($check_if_exists))                //Insert if not exist
    {
        DB::table('webinar_vote')->insert([
                                                    [
                                                        'webinar_id' => $input['uuid'],
                                                        'total_vote' => 0
                                                    ]
                                                ]);
    }

    DB::table('webinar_vote')                          //Incremnt the vote
                ->where('webinar_id', '=', $input['uuid'])
                ->increment('total_vote');
    return 'Vote given successfully';
}

My table is-

enter image description here

I want to have it in model.

My model is-

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class webinar_vote extends Model
{
    protected $table = 'webinar_vote';
    protected $primaryKey='webinar_id';

    public function give_vote()
    {
        //return $this->belongsTo('App\Webinar');
    }
}

But I don't know how to do it in give_vote function.

Can anyone help me please?

Abrar Jahin
  • 13,970
  • 24
  • 112
  • 161

1 Answers1

1

belongsTo is used when you want models connect to each other,in your case its useless as your not trying to do anything related to this ,remove the give_vote method and instead do - webinar_vote::firstOrCreate(['webinar_id' => $input['uuid'])->increment('total_vote'); Make sure that total_vote default value is 0 I would also use a validator to make sure that webinar_id is bigger then 0 and maybe an ip/session check to ignore multiple votes during refresh.

Gal Sisso
  • 1,939
  • 19
  • 20
  • Can u please give me a tailed code, please? I am a little new here, so I can't understand – Abrar Jahin Sep 29 '15 at 05:55
  • The code I gave you could replace all you got inside `add_votes`method [except the `Input:all()` part]. it does exactly the same thing but with much less coding.About the second part is just to protect yourself like webinar bigger then 0 -> as your votes probably starts with index bigger then 0 also you could make sure first that this type of vote exists in your vote table. about the ip/session check you could read here - [Create unique Poll/vote/survey in php](http://stackoverflow.com/a/1042272/3208719) – Gal Sisso Sep 29 '15 at 06:25
  • Hi, I am getting error like this- Class 'App\Http\Controllers\webinar_vote' not found After using the code – Abrar Jahin Sep 29 '15 at 10:37
  • A simple search would have solved your issue.. anyway you will need to include `use \App\webinar_vote;` at the top of your controller or either insert the whole namespace in the method call like - `\App\webinar_vote::` instead of `webinar_vote::` – Gal Sisso Sep 29 '15 at 12:24
  • It is working, thanks, I have upvoted it and accepted it – Abrar Jahin Sep 29 '15 at 15:27