简体   繁体   English

搜索功能:遍历所有数据库表(Laravel)

[英]Search functionality: loop through all database tables (Laravel)

I'm looking for a way to loop through all my database tables in Laravel in order to add a search functionality to my website. 我正在寻找一种在Laravel中循环访问所有数据库表的方法,以便向我的网站添加搜索功能。

I guess there should be a way to do this without hardcoding all the table names. 我猜应该有一种方法可以不对所有表名进行硬编码。

You can do this: 你可以这样做:

$tables = DB::select("select table_name from information_schema.tables where table_schema='your_database_name'");

Just change your_database_name to your own value. 只需将your_database_name更改为您自己的值即可。 Then you can use the Laravel helper function, array_pluck , to get an array of the table_name values. 然后,您可以使用Laravel帮助器函数array_pluck来获取table_name值的数组。

array_pluck($tables, 'table_name')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM