vendredi 2 octobre 2015

Laravel ORM with conditions

I have a table like it-

enter image description here

Model like it-

    <?php

    namespace App;

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

    class webinar_vote extends Model
    {
        protected $fillable = array('webinar_id');
        protected $table = 'webinar_vote';
        protected $primaryKey='webinar_id';
        public $timestamps=false;
    }

What I want is,

  1. If $input['yes_no_value'] is 'yes', then increment vote_yes only
  2. If $input['yes_no_value'] is 'no', then increment vote_no only

And a model query like it-

    if('yes'==$input['yes_no_value'])
    {
        webinar_vote::firstOrCreate([
                                    'webinar_id' => $input['uuid']
                                ])->increment('vote_yes');
    }
    else
    {
        webinar_vote::firstOrCreate([
                                    'webinar_id' => $input['uuid']
                                ])->increment('vote_no');
    }

But the main problem is I can't get the first vote value.

When the entry is create, the data is not increased.

After it is created, it works just fine.

What I am doing wrong?

Can anyone help me please?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire