-
Notifications
You must be signed in to change notification settings - Fork 21
Open
Description
I have 5 models: User, AdmissionTestType, AdmissionTest, AdmissionTestHasCandidate, AdmissionTestOrders
User model:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\DB;
class User extends Authenticatable
{
protected $fillable = [
'username',
'password',
'family_name',
'middle_name',
'given_name',
'gender_id',
'passport_type_id',
'passport_number',
'birthday',
];
public function hasUnusedQuotaAdmissionTestOrder()
{
$orderTable = (new AdmissionTestOrder)->getTable();
$return = $this->hasOne(AdmissionTestOrder::class)
->where('status', 'succeeded')
->whereHas(
'attendedTests', null, '<',
DB::raw("$orderTable.quota")
);
$quotaValidityMonths = config('app.admissionTestQuotaValidityMonths');
if ($quotaValidityMonths) {
$return->leftJoinRelation('attendedTests as attendedTests.type as type')
->where(
DB::raw("
if(
attendedTests.testing_at IS NOT NULL,
DATE_ADD(
attendedTests.testing_at,
INTERVAL type.interval_month + $quotaValidityMonths MONTH
),
DATE_ADD(
$orderTable.created_at,
INTERVAL $quotaValidityMonths MONTH
)
)
"), '>=', now()
);
}
return $return;
}
}
AdmissionTestType model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AdmissionTestType extends Model
{
protected $fillable = [
'name',
'interval_month',
];
}
AdmissionTest model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AdmissionTest extends Model
{
protected $fillable = [
'type_id',
'testing_at',
'maximum_candidates',
];
public function type()
{
return $this->belongsTo(AdmissionTestType::class, 'type_id');
}
}
AdmissionTestHasCandidate model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\Pivot;
class AdmissionTestHasCandidate extends Pivot
{
use HasFactory;
protected $fillable = [
'test_id',
'user_id',
'order_id',
'is_present',
'is_pass',
];
}
AdmissionTestOrders model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class AdmissionTestOrder extends Model
{
protected $fillable = [
'user_id',
'quota',
'status',
];
public function tests()
{
return $this->belongsToMany(AdmissionTest::class, AdmissionTestHasCandidate::class, 'order_id', 'test_id');
}
public function attendedTests()
{
return $this->tests()->where('is_present', true);
}
}
when I use: $user->hasUnusedQuotaAdmissionTestOrder, the id and user_id fields result is null
I need to add ->select(["$orderTable.*"]) on end of the left join to fix id and user_id fields result is null
I think this is not a good idea because if on other file when I want to load relation and load less field that cannot easy to do
other devloper need to open the model to know the relationship need to select {table}.id and {table}.user_id
I have no idea how let it better should be.
Metadata
Metadata
Assignees
Labels
No labels