i have the following models
<?php
namespace AppModelsMastersSystem;
class UserActivationCode extends IlluminateDatabaseEloquentModel
{
public $table = "user_activation_codes";
}
<?php
namespace AppModels;
use AppModelsModel;
use ModulesRequestsModelsEmploymentLeaveType;
use AppModelsMastersSystemUserActivationCode;
class EmployeeEmployment extends IlluminateDatabaseEloquentModel {
public $table = "employee_employments";
public function employeeProfileScore() {
return $this->hasOne('AppModelsEmployeeProfileScore', 'employee_id', 'employee_id');
}
public function test() {
return $this->hasMany(UserActivationCode::class, 'employment_id', 'id');
}
}
in tinker when i run the following commands
$emp = AppModelsEmployeeEmployment::with('test')->whereIn('id', ["4228e839-5ce3-4798-acfd-68958fed3b82"])->first();
$emp->test;
the result is empty array
but when i run without with it returns data
$emp = AppModelsEmployeeEmployment::whereIn('id', ["4228e839-5ce3-4798-acfd-68958fed3b82"])->first();
$emp->test;
i removed everything from model converted it into base state
in query i have the following query running i cant understand why employment_id in 0
[2024-01-24T11:08:27.192268+00:00] Query.INFO: select * from `employee_employments` where `id` in (?) {"Bindings":["4228e839-5ce3-4798-acfd-68958fed3b82"],"Time":2.65,"Origin":[]} []
[2024-01-24T11:08:27.197588+00:00] Query.INFO: select * from user_activation_codes
where user_activation_codes
.employment_id
in (0) {"Bindings":[],"Time":1.29,"Origin":[]} []
2
Answers
it seems the issue was a property of model, You need to add protected $keyType = 'string'; to models with pk that is string.
https://github.com/laravel/framework/issues/30702
Check how your test relationship is being loaded. Is there perhaps a mismatch between the foreign keys and what is set in your database? Make sure that employment_id and id in the user_activation_codes table are of the same type in your database (both UUID if you are using UUIDs).