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

Madan_Para's avatar

How to select all the columns of a table using query

$customer_data = DB::table('users')
                            ->join('child', function($join) use($user_id){
                              $join->on('users.id','=','child.user_id')
                                    ->where('child.user_id', '=', $user_id)
                                    ->select('users.*','child.*');
                            })
                            ->get()->toArray();

I am using this query to select all the columns of 'users' and 'child' table but unable to do this . Can anybody tell me the correct way ?

0 likes
10 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Well first of all the select goes on the main query. Secondly you should probably have the where clause there as wel.

$customer_data = DB::table('users')
                            ->join('child', function($join){
                              $join->on('users.id','=','child.user_id');
                                    
                            })

                            ->where('users.id, '=', $user_id)
                            ->select('users.*','child.*')
                            ->get()->toArray();
1 like
Sinnbeck's avatar

@Madan_Para Please try and explain how it does not work. We have no way of helping out if you dont explain the problem. Do you get an error? Or the wrong data?

Madan_Para's avatar

@Sinnbeck

 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause' (SQL: select * from `users` where `id` = 46 and `name` = Madan limit 1) {"userId":2,"email":"[email protected]","exception":"[object] (Illuminate\Database\QueryException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause' (SQL: select * from `users` where `id` = 46 and `name` = Madan limit 1) at C:\wamp64\www\Project_example\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664, Doctrine\DBAL\Driver\PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause' at C:\wamp64\www\Project_example\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:63, PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause' at C:\wamp64\www\Project_example\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:61)

This is the error i get in laravel log file

Sinnbeck's avatar

@Madan_Para That does not seem to be related to the above query, as that does not have a ->where('name', 'Madan')

1 like
Madan_Para's avatar

@Sinnbeck Sir, i got the answer. There was some mistake in the column name . Thank you sir

Madan_Para's avatar

@Sinnbeck Sir, if i want to join one more table, lets say "requests" table , then how will I do this ? What will be the query ?

Sinnbeck's avatar

@Madan_Para

$customer_data = DB::table('users')
                            ->join('child', function($join){
                              $join->on('users.id','=','child.user_id');
                                    
                            })
                            ->join('requests', function($join){
                              $join->on('users.id','=','requests.user_id');
                                    
                            })

                            ->where('users.id, '=', $user_id)
                            ->select('users.*','child.*')
                            ->get()->toArray();

Oh and its bad practice to just select *. Its much better to only get the columns you actually need

1 like

Please or to participate in this conversation.