iHmD's avatar
Level 1

Get Current Month data in the User model!

I want to get the current month's data, in my controller I can do that like so:

        $myMonthApts = DB::table('appointments')
                        ->where('agent_id', $request->user()->id)
                        ->whereIn('status_id', [3,4])
                        ->whereYear('created_at', Carbon::now()->year)
                        ->whereMonth('created_at', Carbon::now()->month)
                        ->count();

but I Can't do the same thing in the User model using custom functions because I get the error (Method whereYear and whereRaw don't exist)

0 likes
8 replies
topvillas's avatar

It sounds like you need to use query scopes.

But show us your user model.

iHmD's avatar
Level 1
<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\DB;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'fullname', 'username', 'email', 'password', 'role_id', 'active',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function isSupervisor ()
    {
        return $this->role_id === 1;
    }

    public function isAgent ()
    {
        return $this->role_id === 2;
    }

    public function isFirm ()
    {
        return $this->role_id === 3;
    }

    public function group()
    {
        return $this->BelongsTo('App\Group');
    }

    public function role()
    {
        return $this->BelongsTo('App\Role');
    }

    public function appointment()
    {
        return $this->hasMany('App\Appointment', 'agent_id');
    }

    public function unqualifiedAppointments()
    {
        return $this->appointment->where('status_id', 1)->count();
    }

    public function receivedAppointments()
    {
        return $this->appointment->where('status_id', 2)->count();
    }

    public function confirmedAppointments()
    {
        return $this->appointment->where('status_id', 3)->count();
    }

    public function validAppointments()
    {
        return $this->appointment->where('status_id', 4)->count();
    }

    public function canceledAppointments()
    {
        return $this->appointment->where('status_id', 5)->count();
    }

    public function recallAppointments()
    {
        return $this->appointment->where('status_id', 6)->count();
    }
}

for example how can I remodel unqualifiedAppointments function to get this month's records only?

Snapey's avatar
Snapey
Best Answer
Level 122

Obviously you don't have access to the $request, but you should not need it in the model

first, change your appointment relationship to appointments as it is a hasMany

then try

public function myMonthApts()
{
        return $this->appointments()
                        ->whereIn('status_id', [3,4])
                        ->whereYear('created_at', Carbon::now()->year)
                        ->whereMonth('created_at', Carbon::now()->month)
                        ->count();
}
6 likes
iHmD's avatar
Level 1

Thank you so much sir! can you please tell me the difference between $this->appointments() and $this->appointments in my case?

Jonjie's avatar

@snapey I'm just wondering why we still need the ->whereYear() if we already have the Carbon::now()->month?

1 like
Snapey's avatar

@jonjie

ok so you write a query that returns all records where month =08. . this is fine this year, but then in 2021, you query all records where the month is 08 and now you get twice the number you expected because you get all from 08/20 and all from 08/21

1 like
Jonjie's avatar

@snapey Ok. It's clear now. I just thought when you say Carbon::now() it also includes the current year. Anyways, thanks for clearing this up :)

1 like
Snapey's avatar

@jonjie now() returns a full date time string, from which we are plucking the month and year

2 likes

Please or to participate in this conversation.