Edit: For clarity, this is Laravel 5.8.
This is for a HR app I'm working on.
They requested a report to show people who have punched in late. Sure, no problem I thought.
So I have a form with some custom parameters the user can punch in, start_date, end_date, wage, and an array of departments.
public function show()
{
request()->validate([
'start_date' => 'required|date|before_or_equal:today'
]);
$start = Carbon::parse(request('start_date'));
$end = request('end_date') ? Carbon::parse(request('end_date')) : today();
$wage = request('wage');
$departments = request('departments');
$query = EmployeePunch::with([
'employee' => function($query) use ($wage, $departments) {
// IF I UN COMMENT THESE, IN THE FILTER BLOCK BELOW, THE EMPLOYEE BECOMES UNDEFINED.
// if($wage != null) {
// $query->where('hourly', $wage);
// }
// if($departments) {
// $query->whereIn('department_id', $departments);
// }
},
'employee.group',
'employee.department'
])
->whereBetween('punch_time', [$start->startOfDay(), $end->endOfDay()])
// only care about punch in for the day
->where('type', 1);
$results = $query->get();
$latePunches = $results->filter(function ($i) {
$day = strtolower($i->punch_time->format('D'));
$startTime = Carbon::parse(sprintf('%s %s',
$i->punch_time->format('d-m-Y'),
$i->employee->group[$day.'_start_time'])
);
return $i->punch_time->isAfter($startTime)
&& $i->punch_time->diffInMinutes($startTime) >= 5;
});
return view('hr.employeeLateReport.show', compact('latePunches'));
}
So, my problem is in my eager loading and I can't figure this out. If I uncomment the filters in the eager loading of employees, in the filter block near the end of the code block, the $i->employee becomes undefined. If omit the filters, everything works peachy. I've checked the queries being produced and it all looks great.
Any help would be greatly appreciated.
Here's the relationship methods
Employee.php
public function punches()
{
return $this->hasMany(EmployeePunch::class);
}
public function group()
{
return $this->belongsTo(Group::class);
}
public function department()
{
return $this->belongsTo(Department::class)->withDefault();
}
EmployeePunch.php
public function employee()
{
return $this->belongsTo(Employee::class);
}
SQL Output
Try and use whereHas and nest whereBetween:
$query = EmployeePunch::with([
'employee' => function($query) use ($wage, $departments) {
if($wage != null) {
$query->where('hourly', $wage);
}
if($departments) {
$query->whereIn('department_id', $departments);
}
},
'employee.group',
'employee.department'
])->whereHas('employee', function($q) use($start, $end) {
$q->whereBetween('punch_time', [$start->startOfDay(), $end->endOfDay()]);
})->where('type', 1);
Related
I have a problem wanting to pass the id of Products in the subqueries.
The first code is what I have so far. The second is the way I want to do with Eloquent, but I can't.
$result = [];
Product::with(['locals.presentations'])->each(function ($product) use (&$result) {
$body['id'] = $product->id;
$body['nombre'] = $product->nombre;
$sedes = [];
$product->locals->each(function ($local) use (&$sedes, $product) {
$presentations = [];
$local->presentations->each(function ($presentation) use (&$presentations, $local, $product) {
if ($presentation->local_id == $local->id && $presentation->product_id == $product->id) {
$presentations[] = [
'local_id' => $presentation->local_id,
'product_id' => $presentation->product_id,
'presentacion' => $presentation->presentation,
'precio_default' => $presentation->price
];
}
});
...
});
return $result;
I want transform the previous code into this with Eloquent, but I can't pass the product_id into the subqueries:
$products = Product::with(['locals' => function ($locals) {
//How to get the id from Product to pass in the $presentations query ??????
$locals->select('locals.id', 'descripcion')
->with(['presentations' => function ($presentations) {
$presentations
// ->where('presentations.product_id', $product_id?????)
->select(
'presentations.local_id',
'presentations.product_id',
'presentations.id',
'presentation',
'price'
);
}]);
}])->select('products.id', 'nombre')->get();
return $products;
Product
public function locals()
{
return $this->belongsToMany(Local::class)->using(LocalProduct::class)
->withPivot(['id', 'is_active'])
->withTimestamps();
}
Local
public function presentations()
{
return $this->hasManyThrough(
Presentation::class,
LocalProduct::class,
'local_id',
'local_product_id'
);
}
You can simply use the has() method if you have set the relations correctly on the Product and Local models. This will return ONLY the products which has locals AND presentations.
If you want every product but only the locals and presentations with the product_id equals to the products.id, then you don't have to do anything. The relationship you set in your models already checks if the id matches.
$products = Product::has('locals.presentations')
->with(['locals' => function ($locals) {
$locals
->select('locals.id', 'descripcion')
->with(['presentations' => function ($presentations) {
$presentations->select(
'presentations.local_id',
'presentations.product_id',
'presentations.id',
'presentation',
'price'
);
}]);
}])->select('products.id', 'nombre')->get();
I'm in a situation where I need to display the last 5 unique commenters information at the top of the comment list as follows screenshot.
comment image
To do this. I did as follows:
Post Model
public function comments()
{
return $this->hasMany(Comment::class);
}
public function commenter_avatars(){
return $this->comments()->distinct('user_id')
->select('id','post_id','user_id','parent_id')
->whereNull('parent_id')
->with('user')->limit(5);
}
My Controller method as follows
public function index() {
$feeds = auth()->user()
->posts()
->with(['user:id,first_name,last_name,username,avatar', 'media', 'commenter_avatars'])
->orderBy('id', 'desc')
->paginate(10);
return PostResource::collection($feeds);
}
I tried to use groupBy and Distinct.. But did't work as expected.
Did I miss something? or Have there any more best way to solve this?
Thank you in advance!
Noted: I am using latest Laravel (8.48ˆ)
I don't know about your joining of post, user and comments table. But i guess, you can do something similar to following.
At first get latest 5 unique user id of one post:
$userIds = Comments::where("post_id", $post_id)->distinct("user_id")->orderBy("id")
->limit(5)->pluck('user_id');
Then, fetch those user information
$users = Users::whereIn("id", $userIds )->get();
Then, you can return those users
UPDATE
You may use map() to fetch and reorder output. Following is an idea for you:
In Controller:
public function index(Request $request) {
$skipNumber = $request->input("skip"); // this is need for offsetting purpose
$userIds = [];
$feeds = Posts::with("comments")->where("comments.user_id", Auth::id())
->skip($skipNumber)->take(10)->orderBy('comments.id', 'desc')
->map(function ($item) use($userIds){
$users = [];
$count = 0;
foreach($item["comments"] as $comment) {
if(!in_array($comment["user_id"], $userIds) && $count < 5){
$count++;
$userIds.push($comment["user_id"])
$user = User::where("id", $comment["user_id"])->first();
$users.push($user);
}
if($count == 5) break;
}
$data = [
"post" => $item,
"latest_users" => $users
];
return $data;
})->get();
return PostResource::collection($feeds);
}
My code syntax may be slightly wrong. Hopefully you will get the idea.
I have solved this issue by using eloquent-eager-limit
https://github.com/staudenmeir/eloquent-eager-limit
I'm working on an event booking system. When the user searches for a date range, I use a function inside my Event model that checks if the event is available. My search function currently works as follows:
$events = Event::where('name', 'LIKE', '%' . $request->get('term') . '%')
->where('accepted',1)
->orWhere('description', 'LIKE', '%'.$request->get('term').'%')
->orWhere('city', 'LIKE', '%'.$request->get('term').'%')->paginate(15);
$availableEvents = new Collection();
if ($request->get('from') !== '' AND $request->get('to') !== '') {
foreach($events as $key => $event) {
if ($event->is_available($request->get('from'), $request->get('to'))) {
$availableEvents->add($event);
}
}
}
else {
$availableEvents = $events;
}
return view('frontend.events.results', ['events' => $availableEvents, 'request' => $request]);
I can't check availability inside the query builder so I have to loop through. I'm using a Illuminate\Database\Eloquent\Collection because I can't remove events from the paginated thing I get from the query builder.
Is there some way to convert the Eloquent Collection to a paginated collection?
Thanks in advance!
P.S. I'm passing $request to the view to use appends() so I can retain the query string parameters during pagination.
EDIT: The is_available function
public function is_available($from, $to) {
$from = Carbon::createFromFormat('d-m-Y', $from);
$to = Carbon::createFromFormat('d-m-Y', $to);
foreach($this->not_availables as $notAvailable) {
$notAvailableFrom = Carbon::createFromFormat('Y-m-d', $notAvailable->from);
$notAvailableTo = Carbon::createFromFormat('Y-m-d', $notAvailable->to);
if ($from->gte($notAvailableFrom) OR $to->lte($notAvailableTo)) {
return false;
}
}
return true;
}
You can manually create a paginator
You could use Illuminate\Pagination\LengthAwarePaginator like this:
use \Illuminate\Pagination\LengthAwarePaginator;
...
$page = $request->get('page', 1);
$limit = 10;
$paginator = new LengthAwarePaginator(
$availableEvents->forPage($page, $limit), $availableEvents->count(), $limit, $page, ['path' => $request->path()]
);
Then pass the $paginator along with your view, instead of your $availableEvents. In your view you still have $events, you can render the pagination with $events->render(); and pass on the route parameters from $request as you normally would.
I have a model Meetings like this:
public function meeting_comments(){
return $this->hasMany('App\MeetingsComments', 'meeting_id', 'id');
}
public function meeting_users() {
return $this->hasMany('App\UserMeetingDetails', 'meeting_id', 'id');
}
The Controller is like this:
$res = Meetings::with('meeting_comments', 'meeting_users')
->select('')->get()->toArray();
I only need comments from meeting_comments and user_id from meeting_users.
What do I put in select to only get the required fields from meeting_comments and meeting_users ??
You do it through a closure in the with call:
$res = Meetings::with(['meeting_comments' => function($query) {
$query->select('comments', 'meeting_id');
}, 'meeting_users' => function($query) {
$query->select('user_id', 'meeting_id');
}])
->get()->toArray();
I'm taking this from memory, so the syntax may be slightly incorrect, but it should work. :)
I have a following query,
$users = $q->add('Model_User')
->join('profile.user_id', 'id')
->join('activity.profile_id', 'profile.user_id')
->addCondition('timestamp', '>=', date('Y-m-d'))
->addCondition('profile.isActive', true);
->addCondition('activity.isDelivered', false)
->addCondition('activity.priority', '>=', 2);
Now,
I want to traverse all 'users' their 'profiles' and 'activity' associated with each profile.
Relation between User & Profile is 1:n, relation between Profile and Activity is also 1:n.
Is it possible to get something like this?
foreach($users as $user) {
foreach($user->profile as $profile) {
foreach ($profile->activity as $activity) {
//Some actions
}
}
}
Earlier I have used fetching association via ref() but this is slow when number of users are very huge and I don't want to have multiple queries on DB.
My current setup is,
$users = $q->add('Model_User')
->addCondition('timestamp', '>=', date('Y-m-d'));
foreach($users as $user) {
$profiles = $users->ref('Profile', 'user_id');
foreach(profiles as $profile) {
if($profile['isActive']) {
$activities = $profiles->ref('Activity', 'profile_id');
foreach (activities as $activity) {
if(!$activity['isDelivered'] && $activity['priority'] >= 2) {
//Some actions
}
}
}
}
}
Something like this should work:
class Model_User_ForMailing extends Model_User {
function init() {
parent::init();
// join profile, add profile fields you need later
$join_p = $this->join('profile');
$join_p->addField('email');
// join activity, add activity fields you need later
$join_a = $join_p->join('activity');
$join_a->addField('number','act_number');
$join_a->addField('description','act_descr');
// add conditions, important where you add them
$this->addCondition('timestamp', '>=', date('Y-m-d'));
$join_p->addCondition('isActive', true);
$join_a->addCondition('isDelivered', false);
$join_a->addCondition('priority', '>=', 2);
}
function actionSendMail() {
$to = $this->get('email');
$subject = 'New activity #' . $this->get('act_number');
$message = 'You have new activity with this description: ' .
$this->get('act_descr');
mail($to, $subject, $message);
}
}
class mypage extends Page {
function init() {
parent::init();
$m = $this->add('Model_User_ForMailing');
foreach ($m as $junk) {
$m->actionSendMail();
}
}
}
But this is completely untested example. I posted it here just to give you idea how to work with joins and extended models. In this case there should be only one SQL request and only one loop, because you actually need to loop by mails not by users/profiles/activities.