phplaravelunion

laravel eloquent union, join 2 columns in the same table


I have the following columns in the same table

Column 1
--------
1

2

3

Column 2
--------
4

5

6

I want it to be displayed like

Columns
--------
1

2

3

4

5

6

Solution

  • You can use unionAll to make a single trip to the database: PHPize Demo

    $result  = DB::table('yourTable')
        ->select('column1 as res')
        ->unionAll(
            DB::table('yourTable')
                ->select('column2 as res')
        )
        ->get();