Laravel 'belongsTo' 关系无效,不知道为什么
Laravel 'belongsTo' relationship not working, no idea why
我有一个 ClientCourier
class,这是基本迁移:
Schema::create('client_couriers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->unsignedBigInteger('client_id');
$table->foreign('client_id')
->references('id')->on('clients')
->onDelete('cascade');
$table->string('name');
// the same client cannot have two couriers with the same name
$table->unique(['name', 'client_id']);
});
这是模型:
class ClientCourier extends Model
{
protected $guarded = [];
public function client() {
return $this->belongsTo(Client::class);
}
public function services() {
return $this->hasMany(ClientCourierService::class);
}
}
每个 ClientCourier
可以有 man ClientCourierServices
,因此上面的 hasMany
关系。
这是 ClientCourierService
的迁移:
Schema::create('client_courier_services', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->unsignedBigInteger('client_id');
$table->foreign('client_id')
->references('id')->on('clients')
->onDelete('cascade');
$table->unsignedBigInteger('client_courier_id');
$table->foreign('client_courier_id')
->references('id')->on('client_couriers')
->onDelete('cascade');
$table->string('name');
$table->string('code', 64);
// the same courier cannot have two services with the same name
$table->unique(['name', 'client_courier_id']);
// the same client cannot have two services with the same code
$table->unique(['code', 'client_id']);
});
这是模型:
class ClientCourierService extends Model
{
protected $guarded = [];
protected $casts = [
'client_id' => 'integer',
'client_courier_id' => 'integer'
];
public function client() {
return $this->belongsTo(Client::class);
}
public function courier() {
return $this->belongsTo(ClientCourier::class);
}
}
因此,如您所见,我的 client_courier_services
table 外键在 client_courier_id
.
列上带有 client_couriers
现在,当我遍历我的服务并尝试从 Service
中获取匹配的 Courier
时,它不起作用:
$services = ClientCourierService::get();
foreach($services as $service) {
$fromModel = $service->courier;
$fromDb = ClientCourier::where(['id' => $service->client_courier_id]);
// fromModel is null
// fromDb is the correct courier
}
我的其他人际关系正常。通过 $courier->services
从快递公司获取服务是可行的。从 ClientCourier
class 或 ClientCourierService
class 获取客户端是可行的。 $service->courier;
应该对所有帐户都有效,但事实并非如此,我很困惑。
我相信当您使用 courier()
作为方法名称时,它期望列名称是 courier_id
因此没有结果的原因。尝试自己提供列名作为第二个参数。例如:
public function courier() {
return $this->belongsTo(ClientCourier::class, 'client_courier_id', 'id');
}
我有一个 ClientCourier
class,这是基本迁移:
Schema::create('client_couriers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->unsignedBigInteger('client_id');
$table->foreign('client_id')
->references('id')->on('clients')
->onDelete('cascade');
$table->string('name');
// the same client cannot have two couriers with the same name
$table->unique(['name', 'client_id']);
});
这是模型:
class ClientCourier extends Model
{
protected $guarded = [];
public function client() {
return $this->belongsTo(Client::class);
}
public function services() {
return $this->hasMany(ClientCourierService::class);
}
}
每个 ClientCourier
可以有 man ClientCourierServices
,因此上面的 hasMany
关系。
这是 ClientCourierService
的迁移:
Schema::create('client_courier_services', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->unsignedBigInteger('client_id');
$table->foreign('client_id')
->references('id')->on('clients')
->onDelete('cascade');
$table->unsignedBigInteger('client_courier_id');
$table->foreign('client_courier_id')
->references('id')->on('client_couriers')
->onDelete('cascade');
$table->string('name');
$table->string('code', 64);
// the same courier cannot have two services with the same name
$table->unique(['name', 'client_courier_id']);
// the same client cannot have two services with the same code
$table->unique(['code', 'client_id']);
});
这是模型:
class ClientCourierService extends Model
{
protected $guarded = [];
protected $casts = [
'client_id' => 'integer',
'client_courier_id' => 'integer'
];
public function client() {
return $this->belongsTo(Client::class);
}
public function courier() {
return $this->belongsTo(ClientCourier::class);
}
}
因此,如您所见,我的 client_courier_services
table 外键在 client_courier_id
.
client_couriers
现在,当我遍历我的服务并尝试从 Service
中获取匹配的 Courier
时,它不起作用:
$services = ClientCourierService::get();
foreach($services as $service) {
$fromModel = $service->courier;
$fromDb = ClientCourier::where(['id' => $service->client_courier_id]);
// fromModel is null
// fromDb is the correct courier
}
我的其他人际关系正常。通过 $courier->services
从快递公司获取服务是可行的。从 ClientCourier
class 或 ClientCourierService
class 获取客户端是可行的。 $service->courier;
应该对所有帐户都有效,但事实并非如此,我很困惑。
我相信当您使用 courier()
作为方法名称时,它期望列名称是 courier_id
因此没有结果的原因。尝试自己提供列名作为第二个参数。例如:
public function courier() {
return $this->belongsTo(ClientCourier::class, 'client_courier_id', 'id');
}