Search for cross-results in related tables

Asked

Viewed 294 times

1

I created several tables:

Users:

chave primária
email
senha

Physical Detail:

user_id
altura

Personal Data:

user_id
estado_civil

Now I’m having a hard time getting mixed results.

Example: find users with altura = 170 and at the same time with estado_civil = casado.

I tried to group the wheres, but it’s getting a little complex, and worse, the results only bring values from the first table.

  • I could post what you’ve tried ?

  • You must pass the 3 Models to the 3 Tables and the result expected by the visa is very simple your doubt are join and a select with the fields you want to bring, By Usurarioprincipal Model you can bring all the information. So that I do not edit answer pass the models and tables please!

1 answer

2

I create the model but always call what I need in the model. I’ll leave the code only with the part you need

Model According to Laravel’s Website

DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.id', 'contacts.phone', 'orders.price')
            ->get();

Controller Qualquer

$variavel = ModelUsuarios::join('tabeladadospessoais', 'ModelUsuarios.id', '=', 'tabeladadospessoais.user_id')->select('tabeladadospessoais.email', 'tabeladadospessoais.nome', 'tabeladadospessoais.endereco')->get();

Then send for the view.

$this->layout->content = View::make('nome.view')->with('registro',$variavel);

NOTE: Do not forget to use "Use" and put the model name (Modelusuarios).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.