check if there is a record - in a table (EN LARAVEL)

1

In laravel I created a survey system

I have 3 tables

Tabla 1 : Encuesta
Table 2 : Opciones_encuesta
Table 3 : opciones_usuarios
Table 4 : Usuarios

How can I know if I already voted in a poll? How do I access it from Laravel?

My relationship is like this:

Encuesta - opciones_encuesta
(tiene muchas opciones una encuesta)

Opciones_encuesta - Opciones_usuarios

(Una opción tiene muchos usuario que votaron por esa opcion)

Finalmente Usuarios

In the table, (user_options)

I have these fields:

-------------------
|Opciones_usuarios|
|-----------------|
|id               |
|opcion_id        |
|user_id          |
|-----------------|

How do I check if ID_USER is already in this Table? Which option_id  belong to a survey.

How to know if I already voted in that survey?

I do not know if my relationships are well done. I may have the problem there, that's why I can not map correctly.

    
asked by Alex Burke Cooper 30.12.2018 в 09:49
source

1 answer

0

There are several ways.

  • Direct relationship in the models.
  • Query with query builder.
  • Consultation with eloquent
  • Case one

    Survey model.

    <?php
    namespace App //ruta donde este el modelo;
    use Illuminate\Database\Eloquent\Model;
    class Encuesta extends Model 
    {
        protected $table = 'Encuestas';
        protected $primarykey = 'EncuestaID';
    
    }
    

    User model.

    <?php
    namespace App //ruta donde este el modelo;
    use Illuminate\Database\Eloquent\Model;
    class Usuario extends Model 
    {
        protected $table = 'Usuarios';
        protected $primarykey = 'UsuarioID';
    
    }
    

    Pivot models. Model Survey_options

    <?php
    namespace App //ruta donde este el modelo;
    use Illuminate\Database\Eloquent\Model;
    class OpcionEncuesta extends Model 
    {
        protected $table = 'OpcionEncuestas';
        protected $primarykey = 'OpcionEncuestaID';
    
        public function Encuesta()
        {
            return $this->belongsTo(Encuesta::class,'EncuestaID');
        }
    
    }
    

    Model options_users

    <?php
    namespace App //ruta donde este el modelo;
    use Illuminate\Database\Eloquent\Model;
    class OpcionUsuario extends Model 
    {
        protected $table = 'OpcionUsuarios';
        protected $primarykey = 'OpcionUsuarioID';
    
        public function Usuarios()
        {
            return $this->belongsTo(Usuarios::class,'UsuarioID');
        }
    
    }
    

    These models now have the one to many relationship and you can do the following with these models.

    use App\OpcionUsuario; 
    
    $user = OpcionUsuario::Usuarios->where('UsuariosID' ,'>' , 0)->get();
    

    This will return all the information of the user options table plus all the information of the users table.

    Like I could work the other way around you just have to see which way suits you best.

    Option 2

    Using query builder

    $app = \DB::query('SELECT Usuarios.UsuariosID, 
    OpcionesUsuarios.OpcioneUsuarioID  
    FROM Usuarios 
    JOIN OpcionesUsuarios 
    ON Usuarios.UsuariosID = OpcionesUsuarios.OpcioneUsuarioID 
    WHERE Usuarios.UsuariosID > 1 ')->get();
    

    This will bring you all the fields that you request in the select and that comply with the join.

    Third option.

    This way for me is the easiest and as with the models it is much safer than using query builder.

    Eloquent

    $app = Usuarios::where('UsuarioID', '>', 1)
    ->join('OpcionesUsuarios', 'OpcionUsuarioID', '=', 'UsuarioID')
    ->select('Usuarios.UsuarioID', 'OpcionesUsuarios.OpcionUsuarioID')
    ->get()
    

    I hope I could have helped you. If you have doubts about how it works or how you can implement it in your case. Check the laravel documentation.

    For query Builder and eloquent link For relations between models. link

        
    answered by 31.12.2018 в 06:29