lundi 28 septembre 2015

Laravel 5.1 - Controller to Model Convertion

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?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1iXZDEr
via IFTTT

Aucun commentaire:

Enregistrer un commentaire