I have two table users and customer_details. I want to get users which are created by Auth::user(). The created_by column is in customer_details table.
User Table
| id | name | email | status |
|-----------------------------------------------|
| 1 | Admin | admin#email.com | Active |
| 2 | Customer | user#email.com | Active |
CustomerDetails Table
| id | user_id | added_by | address |
|-------------------------------------------|
| 1 | 2 | 1 | NY City |
This is my query
$customers = User::role('Customer')->whereIn('status', ['Active'])->get();
Want to get records where added_by is current auth user
$customers = User::role('Customer')->whereIn('added_by', Auth::id())->get();
I think this should help
here we are using Auth::id() to get the id of the current user and then using eloquent to search for it.
I am considering that here added_by column contains the ID;
Create a relation in your CustomerDetail model:
public function addedBy()
{
return $this->belongsTo(User::class, 'added_by');
}
Then, you can query using this relation:
CustomerDetail::whereHas('addedBy', function($query) {
return $query->where('id', auth()->id());
})->get();
This query returns all customer created by the user current logged in.
To get all users with it's customers created by the current logged in user, add a new relation, now to the user model:
public function customerDetail()
{
return $this->hasOne(CustomerDetail::class, 'added_by');
}
and query users with customers created byt the logged in user:
User::role('Customer')->whereHas('customerDetail', function($query) {
return $query->where('added_by', auth()->id());
})
->whereIn('status', ['Active'])
->get();
Related
i want to get each id's total count from another table named assign job. i saved user data into my users table and i add 3 jobs into assign job table with user id.
but now i want to get total added jobs from assign job table where user ids in my users table at once.
I wrote this function
$assignjob = DB::table('users')
->join('assign_jobs', 'users.user_id', '=', 'assign_jobs.user_id')
->get()
->count();
but this shows me total count in my assign job table. but i want to get different count for each user ids.
i want to assign different color rows with this assign job total count values.
can any one help me.
User Table:
|---------------------|------------------|
| user_id | name |
|---------------------|------------------|
| 123 | john |
|---------------------|------------------|
| 234 | peter |
|---------------------|------------------|
assign job table:
|---------------------|------------------|
| id | user_id |
|---------------------|------------------|
| 1 | 123 |
|---------------------|------------------|
| 2 | 123 |
|---------------------|------------------|
| 3 | 234 |
|---------------------|------------------|
| 4 | 234 |
|---------------------|------------------|
You need to use GROUP BY clause for your user's id column and add COUNT() to your select. Try:
DB::table('users')
->select('users.user_id', DB::raw('COUNT(assign_jobs.user_id) AS jobs_count'))
->join('assign_jobs', 'users.user_id', '=', 'assign_jobs.user_id')
->groupBy('users.user_id')
->get();
Select distinct usesr ID to loop into it to have the count for each users
try this:
add below relation to User Model:
public function assignJobs()
{
return $this->hasMany(AssignJob::class, 'user_id', 'user_id');
}
and in controller:
User::withCount('assignJobs')->get();
I'm new to Laravel, and I got stuck with the following issue:
I have a table for the users, and groups, and a table for connecting them. The general task any user can join any group.
----------------------------------------------
| users | groups | user_groups |
|--------------------------------------------|
| id - int pk | id - pk | id - pk |
| name text | name | user_id - fk |
| email | | group_id - fk |
| phone | | any_attr |
----------------------------------------------
I have the following models:
class User
{
...
public function groups()
{
return $this->belongsToMany(Group::class, 'user_groups')->withPivot(['is_notification_requested']);
}
...
}
class Group
{
...
public function users()
{
return $this->belongsToMany(User::class, 'user_groups');
}
...
}
How do I get all of the groups, with a count of the members? I need the Group model, and a count of the users in the group.
If you're using Laravel 5.3, you can simply add withCount('relationship') as documented here: https://laravel.com/docs/5.3/eloquent-relationships#counting-related-models
Here's an example following your code:
$groups = Group::withCount('users')->get();
Now you can do this:
foreach($groups as $group) {
echo $group->user_count
}
I have a variable called $category which a user selects. On submit I need to look inside a table (profiles) for where the $category matches and pull back all of those profiles then look into the likes table and return all matching records for the returned profiles.
My database looks like:
TABLE: profiles
ID | username | category
-------------------------
1 | x | band
2 | y | airline
3 | z | airline
TABLE: likes
ID | account_id | likes | created_at
---------------------------------------
1 | 2 | 1000 | 21/03/2016
2 | 2 | 2000 | 22/03/2016
3 | 1 | 3000 | 22/03/2016
My code is the following:
$category = "band";
$profiles = DB::table('profiles')
->join('likes', 'profiles.id', '=', 'likes.account_id')
->where('category', '=', $category)
->select('profiles.*', 'likes.likes', 'likes.created_at')
->get();
account_id in the likes table will be the same as id in the profiles table. Also there maybe multiple like records for each profile.
The query just doesn't seem to work and it returns nothing back. I'm using laravel 5.2.
$profiles = DB::table('profiles')
->leftJoin('likes', 'profiles.id', '=', 'likes.account_id')
->where('profiles.category', '=', $category)
->select('profiles.*', 'likes.likes', 'likes.created_at')
->get();
Hi I'm using laravel eloquent for my database query. My Problem is that I can't show the data I wanted to show. I have three tables. Say Users Table, Roles Table and User_Roles table.
Users
id | Name |
1 | John |
2 | Doe |
Roles
id | Name |
1 | Admin |
2 | Employee |
3 | Manager |
User_Roles
id | user_id | role_id
1 | 1 | 2
2 | 1 | 3
3 | 2 | 3
Users has Many Roles that is saved in User_Roles table. My query is "I want to show list of Users that has no Employee roles. In the table I want to show only Doe. How can I make it laravel eloquent.
Hope someone can help!
Define roles relation in User model
public function roles() {
retrun $this->belongsToMany(\Namespace\Role::class);
}
Retrive the role
$role = \Namespace\Role::where('name', 'Employee')->first();
Get the users
$user = \Namespace\User::whereHas('roles', function($query) use ($role) {
$query->where('role_id', '!=', $role->id);
})->get();
My problem: I can't seem to use where('user_id', '<>', Auth::id() on a relationship collection using Laravel's Eloquent ORM.
In Laravel 5, I have a database with the following schema:
| USERS | JOBS | CONVERSATIONS | MESSAGES | CONVERSATION_USER |
| id | id | id | id | conversation_id |
| username | user_id | job_id | conversation_id | user_id |
| password | title | | user_id | |
| | | | message | |
and with the following relationships:
User model:
$jobs = $this->hasMany('job');
$messages = $this->hasMany('message');
$conversations = $this->belongsToMany('conversation');
Job model:
$user = $this->belongsTo('user');
$conversations = $this->hasMany('conversation');
$messages = $this->hasManyThrough('message', 'conversation');
Conversation model:
$job = $this->belongsTo('job');
$messages = $this->hasMany('message');
$user = $this->belongsToMany('user');
Message model:
$user = $this->belongsTo('user');
$conversation = $this->belongsTo('conversation');
I'm trying to get the number of messages for a job, which have not been posted by the authenticated user. My code is:
// Load all of the jobs which relate to the logged in user
$jobs = Job::with(['messages'])->where('user_id', Auth::id())->OrderBy('id', 'DESC')->get();
foreach ($jobs as $job)
{
// Count all messages that have not been sent by the logged in user
echo $job->messages->where('user_id', '<>', Auth::id())->count().'<br />';
}
However I just cannot get the count function to work.
If you change messages-> to message()-> within your loop, you will be able to re-query your relation set.