我撰寫了一個考勤員工系統來加載時間雇主的報告
我得到以下錯誤
我應該怎么做才能解決這個問題?
BadMethodCallException
呼叫未定義的方法 App\Models\User::role()
你的意思是 App\Models\User::only() 嗎?
我的 user.php 模型:
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',
];
}
報告控制器
$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;
}
uj5u.com熱心網友回復:
根據討論,沒有角色表,因此您無法檢查 usingUser::whereHas('role')
而不是,您應該使用以下行
$employees = User::whereRole('employee')->get();
另外,我注意到您沒有在可填充中添加角色,您應該添加角色protected $fillable以考慮該列以執行創建和保存等操作。
uj5u.com熱心網友回復:
您必須role在User模型中定義關系函式。像這樣的東西:
public function role() {
return $this-><Type of relation>(Role::class);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407948.html
標籤:
下一篇:分頁下一頁重繪相同的資料
