laravel eloquent union,在同一个表中加入2列

时间:2018-05-28 07:04:52

标签: laravel union

我在同一张表中有以下列

Column 1
--------
1

2

3

Column 2
--------
4

5

6

我希望它显示为

Columns
--------
1

2

3

4

5

6

3 个答案:

答案 0 :(得分:1)

您可以使用unionAll

i

答案 1 :(得分:0)

假设值在同一行:

$data = DB::table('table_name')->select('column1', 'column2')->get();
$c = count($data);
if ($c) {
    $col1 = [];
    $col2 = [];
    foreach ($data as $k => $d) {
        $col1[] = $d->column1;
        $col2[$c + $k] = $d->column2;
    }
    $result = array_merge($col1, $col2);

    foreach ($result as $value) {
        echo $value."\n";
    }
} else {
    echo "No data in table.";
}

答案 2 :(得分:0)

您可以通过单独取出并在之后合并来制作数据联盟。您可以使用 union()以及 merge()

 use DB;

 public function yourFunction(){
     $data_1 = DB::table('table_name')->get();
     $data_2 = DB::tabele('table_name')->get();
     //your final data
     $final_data = $data_1->union($data_2);
 }