Laravel 5.5: Pick name of each table in select Union

Asked

Viewed 191 times

1

I need to make a select in two tables and join them with the union, until then, but I also need to take the name of the two View.

I’ve tried using the getTable() in each select, but it returns me the error:

Non-static method Illuminate\Database\Eloquent\Model::getTable() should not be called statically

I also tried to pick up the select, because I’ve done it this way once without using the Laravel: select('tabela1 as table') and gives me a syntax error.

What’s the right way to do it?

Code:

$first = Lista::where('serie_id', $serie->id)
        ->select('descricao as result1');

$atividades = Avaliar::where('serie_id', $serie->id)
        ->select('nota as result1')
        ->union($first)
        ->get();
  • Do you want to get the names of the tables in UNION? I ended up not understanding.?

  • Exactly, because I’ll need to identify them in the view.

  • In the View with so??? if speaks the file?

  • Yes. Simply put, let’s say that the results coming from the list table will be red and those coming from the table will be blue.

  • I get it @Diego, and if I’m identified in the SQL?

  • I don’t understand, what do you mean?

  • I can identify in SQL itself the table name explicitly!

  • 1

    Your example there worked well.

Show 4 more comments

1 answer

1


In itself SQL create the identification of each SELECT, example:

$first = Lista::where('serie_id', $serie->id)
        ->select('descricao as result1')
        ->selectRaw("'Lista' as tabela");

$atividades = Avaliar::where('serie_id', $serie->id)
        ->select('nota as result1')
        ->selectRaw("'Avaliar' as tabela");
        ->union($first)            
        ->get();

Browser other questions tagged

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