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