馆藏中未找到的一些相关记录

Some related records not found in collections

我有以下查询在 laravel 5.4 中运行良好,但是在迁移到新服务器并更新到 laravel 8 之后,一些相关记录没有链接到集合和注册中- >客户 return 为空。

奇怪的是,我可以创建一个连接查询并以这种方式获得相关结果,并且大多数记录都正常运行,return 他们的相关客户模型数据,只有大约 10% 的记录不正常工作。我感觉这是服务器设置或 mysql 设置的问题,但它真的很奇怪。

$query = Paymentplan::query()->wherehas('orders', function($q){
    $q->where('course_id','<', '99');
});

$registrations = $query->with('orders', 'customers')->orderby('paymentplans.created_at', 'desc')->paginate(50)->appends(request()->query());

付款计划模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Customer;
use App\Models\CustomerPromotions;
use \Venturecraft\Revisionable\RevisionableTrait;

class PaymentPlan extends Model
{
    
    use RevisionableTrait;
    use Uuids;

    use RevisionableTrait, Uuids {
        Uuids::boot insteadof RevisionableTrait;
    }

     /**
    * Indicates if the IDs are auto-incrementing.
    *
    * @var bool
    */
    public $incrementing = false;
    
    protected $table = 'paymentplans';
    
    public function customers()
    {
        return $this->belongsTo(Customer::class, 'customer_id');
    }
}

客户Table:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Order;
use App\Models\PaymentPlan;
use App\Models\PaidInFull;
use App\Models\Course;
use App\Models\DeliveryMode;
use App\Models\Region;
use \Venturecraft\Revisionable\RevisionableTrait;




class Customer extends Model
{
    use RevisionableTrait;
    use Uuids;

    use RevisionableTrait, Uuids {
        Uuids::boot insteadof RevisionableTrait;
    }
    
    /**
    * Indicates if the IDs are auto-incrementing.
    *
    * @var bool
    */
    public $incrementing = false;
    
    protected $table = 'customers';

    protected $dontKeepRevisionOf = ['LastLogin'];

    public function paymentplans()
    {
        return $this->hasMany(PaymentPlan::class)->orderBy('created_at', 'desc');
    }
}

所以问题与 UUID 相关 - 在 laravel 8 中,我需要在客户模型中声明 $keytype = string。我就是这样修复的。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Order;
use App\Models\PaymentPlan;
use App\Models\PaidInFull;
use App\Models\Course;
use App\Models\DeliveryMode;
use App\Models\Region;
use \Venturecraft\Revisionable\RevisionableTrait;




class Customer extends Model
{
    use RevisionableTrait;
    use Uuids;

    use RevisionableTrait, Uuids {
        Uuids::boot insteadof RevisionableTrait;
    }
    
    /**
    * Indicates if the IDs are auto-incrementing.
    *
    * @var bool
    */
    public $incrementing = false;
    protected $keyType = 'string';
    protected $table = 'customers';
    public function paymentplans()
    {
        return $this->hasMany(PaymentPlan::class)->orderBy('created_at', 'desc');
    }
}
    ```