Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Maison012's avatar

Fetch data from multiple tables

Hello im new on laravel. And im working on a project when i need to fetch some data from many tables on laravel. Until now i can get data from one table. In fact my problem is a bit complicated and maybe i cant explain very good. So: I need to fetch data from many table

	 				| cus_1001                   |
					| cus_1002                   |
					| cus_1003                   |
					| logs                          |
					| migrations                    |
					| oauth_access_tokens           |
					| oauth_auth_codes              |
					| oauth_clients                 |
					| oauth_personal_access_clients |
					| oauth_refresh_tokens          |
					password_resets               |
					| permission_role               |
					| permissions                   |
					| role_sondazhe                 |
					| role_user                     |
					| roles                         |
					| primtable                     |
					| users

This is my mysql structure. Normally i can fetch data from primtable . I now want to get the data from the tables cus_1001, cus_1002, cus_1003. And add this to primtable. My biggest problem is that all the time these tables cus_1001 ... 1003, will change constantly and in order the numbers will be increasing to infinity (cus _ ++). Any one have an idea how can i select data from this cus _ ++ table and to send on primtable on sync?

0 likes
6 replies
bugsysha's avatar

First, execute the SHOW TABLES; query, and then you can filter by name to get all existing tables and query them one by one. Just note that is a very bad architecture and it will cause you problems in a long run.

1 like
Maison012's avatar

@bugsysha i need to do this on laravel. What is bad architecture, are you meaning mysql db?

bugsysha's avatar

@usertxr yes, you do it in Laravel.

DB::select('SHOW TABLES');

And yes, your database is in very bad shape. That is not smart and hard to sustain.

Maison012's avatar

I have changed my db structure.. And now iam trying to transfer data from a table to another but still i have a problem with this couse i can send data to new table..

This is function on controller

		$v_list = DB::table('v_lists')->where('status', '=', 'Y')->get();
   		$fields = $v_list->id;

    	$custom_ = "custom_";
    	$list_active = [ $custom_, $fields ];
   		 foreach ($list_active as $lactive) {
        	   DB::table('v_lists')
            	->select('v_lists.id','v_lists.status','v_lists.interesa')
            	->join('v_custom_fileds', 'v_lists.id', '=', 'v_custom_fileds.custom_id')
            	->get();
        }

V_list is my main table where im trying to get data who haz status = 'Y' and to trasfer to v_custom_fileds.

this is v_list structure

					 +----------+-------------+------+-----+---------+-------+
					 | Field    | Type        | Null | Key | Default | Extra |
					 +----------+-------------+------+-----+---------+-------+
					  | id       | int                | NO  | PRI | NULL    |       |
					  | status   | varchar(45) | NO   |     | NULL    |       |
					  | interesa | varchar(45) | YES  |     | NULL    |       |
					  +----------+-------------+------+-----+---------+-------+

ant this v_custom_fileds

					+-----------+-------------+------+-----+---------+-------+
					 | Field     | Type        | Null | Key | Default | Extra |
					  +-----------+-------------+------+-----+---------+-------+
					  | custom_id | int         | NO   | PRI | NULL    |       |
					  | status    | varchar(45) | YES  |     | NULL    |       |
					  | interesa  | varchar(45) | YES  |     | NULL    |       |

+-----------+-------------+------+-----+---------+-------+

I want when data to go on v_custom_filed id to be from 1 to custom_1

Please or to participate in this conversation.