Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel从两个表中获取数据,不使用分页连接_Php_Mysql_Laravel_Union All_Laravel Pagination - Fatal编程技术网

Php Laravel从两个表中获取数据,不使用分页连接

Php Laravel从两个表中获取数据,不使用分页连接,php,mysql,laravel,union-all,laravel-pagination,Php,Mysql,Laravel,Union All,Laravel Pagination,我想从两个表properties和properties\u x中获取结果,其中properties.address或properties\u x.address\u x类似于test,使用laravel分页 这两个表之间没有外键关系 properties id name address 1 test1 2 test2 3 test3 4 test3 test 5 test4 test properties_x id name a

我想从两个表propertiesproperties\u x中获取结果,其中
properties.address
properties\u x.address\u x
类似于
test
,使用laravel分页

这两个表之间没有外键关系

properties

id  name    address
1   test1   
2   test2   
3   test3   
4   test3   test
5   test4   test

properties_x

id  name    address
1   test1_x test
2   test2_x 
3   test3_x 
4   test3_x test
5   test4_x 

    Expected results:
    name     address
    test3    test
    test4    test
    test1_x  test
    test3_x  test

使用
union all
合并两个表的数据

并从数据库中的这些数据中获取列,因此可以使用分页

试着这样做:

$p1=DB::table('properties')
->其中('地址','测试')
->选择(‘名称’、‘地址’);
$p2=DB::表('properties_x')
->其中('地址','测试')
->选择(‘名称’、‘地址’);
$p=$p1->unionAll($p2);
DB::table(DB::raw(({$p->toSql()})作为p”))
->合并绑定($p)
->选择('名称','地址')
->分页(10);

我不知道是否还有其他方法,但它对我很有效

$res= [];

$tableONe = Property::where('address','test')->get();

array_push($res,$tableOne);


$tableTwo = PropertyX::where('address','test')->get();

array_push($res,$tableTwo);

现在$res将这两个数据表放在一起

union all,然后laravel查询生成器为mysql union提供unionAll方法。当您在进行大型项目或ERP级别的项目时,通常需要使用union从具有多个表的数据库中获取数据。在下面的示例中,您可以看到如何在Laravel5中使用UNIONALL

例如:

$silver = DB::table("product_silver")
    ->select("product_silver.name"
      ,"product_silver.price"
      ,"product_silver.quantity");
$gold = DB::table("product_gold")
    ->select("product_gold.name"
      ,"product_gold.price"
      ,"product_gold.quantity")
    ->unionAll($silver)
    ->get();
print_r($gold);

代码格式化和句子改进但我需要数据pagination@HarpalSingh是的,您可以,但您需要选择相同名称、相同顺序和相同计数的列。您可以为我提供相同的语法吗?例如
$p3=DB::table('properties_x3')->select('title as name','address')$p=$p1->unionAll($p2)->unionAll($p3)@HarpalSingh很高兴能帮上忙