I want to make a list of all users that created an item.
These items have the row user_id
To get all items created today, I do this:
$items = Item::whereDay('created_at', '=', date('d'));
Now I want to perform a query that gives me all users who have created an item today. There will be users who created 1000 items, and there will be users that didn't do anything at all.
I could do this with ->unique() but there must surely be a better way to do this.
Any suggestions?
Assuming you have the items relationship set on your User model you could do something like:
$users = User::whereHas('items', function ($query) {
$query->where('created_at', '>=', \Carbon\Carbon::today());
})->get();
Related
I need to create a wishlist with laravel, I already use spatie/query-builder to get products.
At this time everything works fine and I can retrieve products already added to wishlist.
I need help for reverse the process.
Three tables are used :
users
products
products_favorites (pivot table) with user_id and product_id
If record exists in pivot table product is in wishlist
So with this custom query builder filter :
return $query->join('product_favorites', function ($query) {
$query->on('product_favorites.product_id', '=', 'products.id');
})->where('product_favorites.user_id', '=', Auth::id());
I can get products who are in current user wishlist. I want to reverse the process.
Get all products who aren't in product_favorites table.
How do you think I can achieve this ?
Thank's for your help
I'm not sure but this on seems to work :
return $query->leftJoin('product_favorites', function ($query) {
$query->on('product_favorites.product_id', '=', 'products.id');
})->where('product_favorites.user_id', '=', null);
I want to fetch data from two table profile and User they are link with one to one Relationship i'm fetch data from both table but now i want to add where condition that column in user table.
this code working fine for fetch data.
$clients = Profile::all();
return view('admin.clients', compact('clients'));
But i want some thing like this.
'select profile.*, user.* from profile, user where profile.user_id=users.id and users.role_id=3';
how to convert this query in shape of above query.
You could add a where in your query relationship or in the relationship itself.
lets say you have a users relationship you could do this
Profile::with(['users' => function($users) {
$users->where('role_id','=',3);
}])->get();
This will get you all the profiles, and filter the users that has the role = 3 of those profiles.
if what you want is to get the profiles of the users that has a role = 3 then you use whereHas
Profile::with('users')->whereHas('users', function($users) {
$users->where('role_id','=',3);
})
->get();
If you are fetch data with relational tables and use where condition in query write this code it's 100% working for me.
Profile::join('users', function($join){
$join->on('profiles.user_id', '=', 'users.id')
->where('users.role_id', '=', 3);
})
->get();
using join i'm successfully fetch all profiles where users role id equal 3 with other relational tables data like email from users table and service title from service table along with all data from profile table
I have a table users in database
id, name, users_type
there are multiple types of users_type like Admin, Sub Admin, Unsigned, Normal User
I want to get All Types of users_type But Not Admin user.
If you are using eloquent Model then
$users = User::where('users_type', '!=', 'Admin')->get();
Using Query Builder
$users = DB::table('users')->where('users_type', '!=', 'Admin')->get();
check this for query builder https://laravel.com/docs/5.6/queries and for Eloquent you can look this https://laravel.com/docs/5.6/eloquent
$users = User::where('users_type','<>','Admin')->get();
You want to get result for equals to then no need add =
If you want to get only one user then use first() insted of get()
I am working on some reporting for an app that has users and bookings.
The aim is to return all users that haven't made a booking within the last 2 months.
My query currently returns all users who has ever made a booking at the given team and order the bookings by newest to oldest.
My question is how can I only return users where the last booking made has an estimated booking time more than 2 months ago?
My current query is:
$collection = User::whereHas('customerBookings', function($q) use ($teamId) {
$q->where('team_id', $teamId);
})->with(['customerBookings' => function ($q) use ($teamId) {
$q->where('team_id', $teamId);
$q->orderBy('estimated_booking_time', 'desc');
}])
->get();
This correctly returns all users that have bookings (relationship to a bookings table) that have the requested team_id and then only return the bookings that match the same requirements.
I was going to use ->reject on the collection but I would rather just filter out in the query itself.
Ideally I want to add something to the with claus:
$q->where( FIRST RELATIONSHIP ITEM ->estimated_booking_time, '<=', Carbon::now()->subMonths(2) );
The issue for me is how I can get the first item from the relationship and use it to only return the users depending on this last item.
You can just put in where() the time of last booked (e.g. 2months ago) minus the time now. You may also have a look at this reference. I think this has similar to yours.
Probably you have a has-many relationship between the User and the Booking models.
The User model does have a "bookings()" method related to the Booking model.
Said that, you can do:
$users = \App\User::whereHas('bookings', function ($query) {
$query->where('lestimated_booking_time', '<=', Carbon::now()->subMonths(2));
});
I have a list or products in my database that needs to be ordered based on their updated_at field. But I also want to view the products at the beginning that are missing some attributes.
For example, I want to list those products at first that are missing description or colors and then order them based on their update date. Also I want to paginate them so I cannot sort them after I retrieve all the products from database.
So is there a way to achieve this using eloquent in Laravel?
This is how my products table looks like,
products
-id
-style
-title
-description
-colors
-sizes
-cost
-weight
-created_at
-updated_at
You can use limit and skip for pagination not sure but you can use like this for your problem..
function getProdcucts($skip, $limit){
$list = DB::table('reports')->select()
->where('colors', '=', null)
->orWhere('description', '=', null)
->orderBy('updated_at', 'desc')
->skip($skip)
->limit($limit)
->get();
return $list;
}
Dont fetch all record in one time, fetch record from db according to per page items.