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 ?
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();
@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?
@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
@Madan_Para That does not seem to be related to the above query, as that does not have a ->where('name', 'Madan')
@Sinnbeck Sir, i got the answer. There was some mistake in the column name . Thank you sir
@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 ?
@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
@Sinnbeck Thanks for helping me sir. Ok sir, i will follow those tips.
Please sign in or create an account to participate in this conversation.