I tried to match two columns and if it was matching based on the id i'll assign username to that matched column. but inside join i can't make if condition on joining table.
$list = DB::table('job')
->join('users', 'users.id', '=', 'job.created_by')
->select('job.*','users.first_name as created_by','users.first_name')
->where('job.enterprise_id', $user->id)
->get();
In my json i had one value updated_by with value 1 means i can match with id column if it was matching i will display updated_by = first_name otherwise updated_by = null.
To show the name of user who updated job you can use a left join to users table.
$list = DB::table('job as j')
->join('users as u1', 'u1.id', '=', 'j.created_by')
->leftJoin('users as u2', 'j.updated_by' , '=', 'u2.id')
->select('j.*','u1.first_name as created_by','u2.first_name as updated_by')
->where('j.enterprise_id', $user->id)
->get();
If job was updated u2.first_name will return the name of user who updated that job if job is not update you will have a null value against u2.first_name. Also i assume you job table has a column named as updated_by which stores this information.
Related
The following is my query which may output more than one row. I need to store the user_id field in each row in the $send_user_id variable below, so that I will use a foreach loop in the blade. I'm currently receiving (Trying to get property of non-object) error.
$j_request = DB::table('grpusrs')
->join('users', 'users.id', '=', 'grpusrs.user_id')
->join('groups', 'grpusrs.group_id', '=', 'groups.id')
->where('groups.owner_id', $user_id)
->where('grpusrs.join_request', 1)
->get();
$send_user_id = $j_request->user_id;
Also this query joins two tables, so how do I specify the table that has the required field? (i.e. I want the user_id that is in the 'Grpusrs' table not in the 'Groups' table)
If you need to retrieve multiple user_id you can loop through $j_request object and collect every user_id inside an array.
$j_request = DB::table('grpusrs')
->join('users', 'users.id', '=', 'grpusrs.user_id')
->join('groups', 'grpusrs.group_id', '=', 'groups.id')
->where('groups.owner_id', $user_id)
->where('grpusrs.join_request', 1)
->select('users.*', 'contacts.phone', 'orders.price')
->get();
$send_user_id = [];
$j_request->each(function($item) use ($send_use_id){
$send_user_id[] = $item->user_id;
});
If you only need one user_id you should be more "precisely" in your query (get just one record). After that, you can get user_id simply with $j_request->user_id
I considered an issue when viewing data called from database using two related tables users and roles which have this structure:
the users.role column contains an integer referring to the role record of roles table
the roles.name column contains the role name i.e. (user / admin / editor)
in my controller I used the laravel docs to build my query as I need to show a table in blade that holds users.name , users.email and roles.name that related to user by the schema:
$users = DB::table('users')
->join('roles', 'users.role', '=', 'roles.id')
->select('users.name', 'users.email', 'roles.name')
->get();
actually it works and dumping data, but the issue is the confusion of the two name alike cols users.name and roles.name. it dumping only the roles.name value like this sample record:
{"name":"user","email":"user#asd.com"},{"name":"user","email":"new#asd.com"},{"name":"user","email":"jaeden93#example.org"}
even when I tried to select all cols of users table like below:
$users = DB::table('users')
->join('roles', 'users.role', '=', 'roles.id')
->select('users.*', 'roles.name')
->get();
the users.name col still not showing!
Is there a way to solve this issue without changing table's column titles ?
You can give an alias to the column, to avoid problems with the same name
Use this
$users = DB::table('users')
->join('roles', 'users.role', '=', 'roles.id')
->select('users.*', \DB::raw('roles.name as role_name'))
->get();
It's the same to do this in SQL
SELECT users.*, roles.name as role_name FROM ...
I've searched and tried a lot, but unfortunately, I didn't solve my problem. So, I'm posting my question here, please look and suggest me some solution.
I have few tables to manage users like users, profiles, groups, and group_user. Now, I want to retrieve all user name which are not the member of a particular group.
Efforts
$users = DB::table('users')
->join('profiles', 'users.id', '=', 'profiles.user_id')
->join('group_user AS gu', 'gu.user_id', '!=', 'users.id')
->join('groups', 'groups.id', '=', 'gu.group_id')
->where('groups.label', '=', $grouplabel)
->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');
I'm executing the query above to get list of users which are not the member of a particular group, but I'm not able to solve it 'til now.
If I change != to = then I get the result of all users who are in the particular group.
Table record and structure in the image.
As, you can see I have 5 users, out of which 3 users having admin group and 2 are not. If I run the query for admin group, then there should be remaining 2 user or if I run query for test group then I should get 5 users.
you don't need to change the join part, all you need to change or add up in your where() section
$users = DB::table('users')
->join('profiles', 'users.id', '=', 'profiles.user_id')
->join('group_user AS gu', 'gu.user_id', '=', 'users.id') // mark it as '='
->join('groups', 'groups.id', '=', 'gu.group_id')
->where('groups.label', '<>', $grouplabel) // change here, see below description
->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');
this will ignore the particular group (eg: profile) and will return all other users except this group (profile)
Edited
I just removed the profile section from query, to verify the relationships, once you get the results then we will add up profile section, just run this query
select * from users as u
inner join group_user as gu
on
gu.user_id = u.id
inner join groups as g
on
g.id = gu.group_id where
groups.label <> 'Tester'
Edited 2
$users = DB::table('users')
->join('profiles', 'users.id', '=', 'profiles.user_id')
->join('group_user AS gu', 'gu.user_id', '=', 'users.id')
->join('groups', 'groups.id', '=', 'gu.group_id');
//if users exists in specific group
if($users->where('groups.label', '=', $grouplabel)->count() > 0 ){
$result = $users->where('groups.label', '=', $grouplabel)
->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');
} // return all users
else{
$result = $users->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');
}
Okay I have a some code written to retrieve data from some left join in my database, after retrieving this data I use the id from each to populate a modal. The thing is it works for some of the data that has been retrieve. For when i look at the query result I notice the id's are null for some valid data an some or not
$someuser = DB::table('users')
->join('sometable', 'sometable.user_id', '=','users.id')
->leftjoin('sometable', 'users.id', '=', 'skills.user_id')
->leftjoin('sometable', 'users.id', '=','schools.user_id')
->leftjoin('sometable', 'users.id', '=', 'languages.user_id')
->leftjoin('sometable','users.id', '=', 'memberships.user_id')
->leftjoin('sometable', 'users.id', '=', 'convictions.user_id')
->leftjoin('sometable', 'users.id', '=', 'work_experiences.user_id')
->select('users.*','sometable.name as sometablename','sometable.*')
->where('users.first_name', '<>', '')->whereNotNull('users.id')
->orderBy('sometable.updated_at', 'DESC')->groupby('users.first-name')->distinct()->paginate(100);
from that, the query result looks like:
{"total":966,"per_page":100,"current_page":1,"last_page":10,"from":1,"to":100,"data":[{"id":null, etc
What am simple saying why is my id null
I think its not showing me no ids over 1900 so if a user is over 1900 it showing me null
You are selecting * from multiple tables. Since you are also left joining, it's very likely there is nothing to join on so your database is returning those columns as null. Specifically select the id you need and alias it as something which will not be overwritten by another column.
I have this reference code below, first the line 1 will get
$aa = users_details::select(array('id', 'user_id'))->where('id', '=', $request->id)->first(); //first line
$bb = User::select(array('user_id'))->where('user_id', '=', $aa->id)->with('user_details')->first(); //second line
the first line returns 13 which is correct while the second line returns 'null' which it should not because there's actually a record that has a user_id of '13'. Any ideas, help please?
and also is there a way I could select only selected columns in this join table (Eloquent relationship hasOne)
$bb = User::select(array('user_id'))->where('user_id', '=', $aa->id)->with('user_details')->first();
I dont know if
select(array('user_id'))
is the right one to select only selected columns from the join table. Any ideas, help, please?
First Question:
$bb = User::select(array('user_id'))->where('user_id', '=', $aa->id)->with('user_details')->first();
Do you have user_id column in users (or anywhere User Model refers to) table? Do you have user_details method in User model?
Second Question:
You can select as
User::select('user_id', 'username', 'sex')
If you have same column name in the joined table, then you can select as
User::select('users.user_id as a', 'users.username', 'users.sex', 'user_details.user_id as b')
UPDATE:
How can I select only selected column from the 'user_details' when
both is joining togehter?
User::select('users.user_id as user_id', 'users.username', 'users.sex', 'user_details.user_id as user_details_id')->join('user_details','user_details.user_id','=', 'users.id' )->where('users.id', '=', $aa->id)->first();
To select the columns from the joined table, just prefix the column name with table name such as user_details.user_id.