Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

BadMethodCallException Call to undefined method App\Models\User::role()

I code a attendance employee system to load reports of time employers

i got below error
What should I do to solve this problem?

BadMethodCallException
Call to undefined method App\Models\User::role()
Did you mean App\Models\User::only() ?

my user.php model:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;

    use Notifiable;

    protected $fillable = [
        'name',
        'email',
        'password',
    ];

   

   
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

   
    protected $appends = [
        'profile_photo_url',
    ];
}

Report Controller

        $employees = User::whereHas('role', function ($query) {
            $query->where('employee');
        })
            ->get();
        $dateRange = $reportService->generateDateRange();

        $timeEntries = $reportService->generateReport($request->input('employee'));
        if ($timeEntries) {
            $chart = new LaravelChart([
                'chart_title'           => 'Hours of work per day',
                'chart_type'            => 'line',
                'report_type'           => 'group_by_date',
                'model'                 => 'App\\TimeEntry',
                'group_by_field'        => 'time_start',
                'group_by_period'       => 'day',
                'aggregate_function'    => 'sum',
                'aggregate_field'       => 'total_time_chart',
                'column_class'          => 'col-md-8',
                'continuous_time'       => true,
            ]);
        } else {
            $chart = NULL;
        }

       

>Solution :

as per the discussion, there is no roles table so you can’t check using User::whereHas('role')
instead of that, you should use the following line

$employees = User::whereRole('employee')->get();

also, I have noticed that you haven’t added role in fillable, you should add role to protected $fillable in order to consider that column for performing operations like create and save.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading