Queries with laravel and eloquent

1

Good day, I'm starting with laravel and I developed a page to practice.

I have these two models:

class Personas extends Model{

      protected $table = 'persona';
      protected $primaryKey = 'id';
      public $timestamps = false;
      protected $fillable = [
                 'nombre',
                 'apellido', 
                 'dni', 
                 'direccion'
               ];
      public function empleado(){

          return $this->hasOne('Practica\Models\Empleado', 'id_persona', 'id');
      }
}


class Empleado extends Model{

       protected $table = 'empleados';
       protected $primaryKey = 'id_persona';
       public $timestamps = false;
       protected $fillable = [
                 'id_persona',
                 'id_cargo', 
                 'sueldo',
                 'ficha'
               ];
       public function persona(){
          return $this->belongsTo('Practica\Models\Persona', 'id_persona', 'id');
       }
}

I have people who are employees and also clients, but at this moment I want to get all the people who are clients.

How could this query be done using eloquent orm of laravel ?

    
asked by unprogamador2000 03.10.2016 в 10:56
source

2 answers

0

Here is an example of how you can do it:

$persona= Persona:find(1);

$empleados = $empleados->empleados()->whereHas('id_persona', function($q){
    $q->where('id', $persona->id);
})->get();
    
answered by 03.10.2016 в 14:12
0

The response from a controller would be something like:

public function store(Request $request){
    $clientes= new App\Clientes::orderBy('id','ASC');
    dd($clientes->personas);      
}

But you are starting from a bad design. There you link the clients with their people nothing more.

    
answered by 03.10.2016 в 14:09