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

yonka's avatar
Level 2

Listing up all tables from DB without knowing the db

I want to get list of all tables without knowing the db, I want to select the table I want in order to get all softdeleted records from that table using laravel Livewire

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To list all tables from a database without explicitly knowing the database name, you can use Laravel's database connection to query the information schema. The information_schema.tables table contains metadata about all tables in the database.

Here's how you can achieve this in Laravel:

  1. Query the Information Schema: You can use the DB facade to run a raw SQL query to get all table names.

  2. Filter for Soft Deleted Records: Once you have the table names, you can select a specific table and query for soft-deleted records if the table uses soft deletes.

Here's a step-by-step solution:

Explanation:

  • listTables Method: This method retrieves all table names from the current database using the SHOW TABLES SQL command. It then maps the result to extract just the table names.

  • getSoftDeletedRecords Method: This method checks if the specified table exists and then queries for records where the deleted_at column is not null, which indicates soft-deleted records. This assumes that the table uses Laravel's soft delete feature.

Note:

  • Ensure that your database user has the necessary permissions to access the information_schema and perform the SHOW TABLES command.
  • The deleted_at column is a convention used by Laravel's soft delete feature. If your tables use a different column name for soft deletes, adjust the query accordingly.

Please or to participate in this conversation.