Laravel: 当客户有多个记录时更新问题
Laravel: update issue while customer has multiple records
我将这三个 table device_companies
、devices
和 device_inventory
与关系连接起来,并在创建和更新客户时加载了 ajax。但是,仅加载由 subscriber_devices
table.
管理的可用数据
create
部分按要求工作。但是,在更新与所选客户关联的所有 devices
时正在加载。例如:如果 customer
有 ABC -> DEF -> A1,A2,A3
而我正在更新 A2
的详细信息,那么 A1,A3
不得出现在下拉列表中。只有 A2
和 A4,A5,A6,...
应该加载。
表格:
device_companies: -> devices: -> device_inventories:
|-- id |-- id |-- id
|-- title |-- device_company_id |-- device_id
... |-- title |-- serial_number
... ...
subscriber_devices:
|-- id
|-- subscriber_id
|-- device_inventory_id
...
设备公司:
public function devices()
{
return $this->hasMany(Device::class);
}
public function _dropdown($useWith = null, $subscriber = null)
{
$Self = self::where('status', \Common::STATUS_ACTIVE)->select([
'id',
'title'
])->orderBy('title', 'ASC');
# with relations
if($useWith == true)
$Self = $Self->whereHas('devices.deviceInventory', function($q) use ($subscriber) {
$q->whereDoesntHave('subscriberDevice', function($q) use ($subscriber) {
if(!is_null($subscriber))
$q->where('subscriber_id', '!=', $subscriber->subscriber_id);
});
});
# building query
$Self = $Self->get();
return ($Self->isEmpty() == false)
? $Self->toArray()
: null;
}
设备:
public function deviceCompany()
{
return $this->belongsTo(DeviceCompany::class);
}
public function deviceInventory()
{
return $this
->hasMany(DeviceInventory::class)
->where('status', \Common::STATUS_ACTIVE)
->orderBy('serial_number', 'ASC');
}
public function _dropdown($deviceCompanyId, $useWith = null, $subscriber = null)
{
$Self = self::select([ 'id', 'title' ])->where([
'device_company_id' => $deviceCompanyId,
'status' => \Common::STATUS_ACTIVE
])->orderBy('title', 'ASC');
# with relation
if($useWith == true)
$Self = $Self->whereHas('deviceInventory', function($q) use ($subscriber) {
$q->whereDoesntHave('subscriberDevice', function($q) use ($subscriber) {
if(!is_null($subscriber))
$q->where('subscriber_id', '!=', $subscriber->subscriber_id);
});
});
# building query
$Self = $Self->get();
return ($Self->isEmpty() == false)
? $Self->toArray()
: null;
}
设备清单:
public function subscriberDevice()
{
return $this->hasOne(SubscriberDevice::class);
}
public function _dropdown($deviceId, $useWith = null, $subscriber = null)
{
$Self = self::select([
'id',
'serial_number AS title'
])->where([
'device_id' => $deviceId,
'status' => \Common::STATUS_ACTIVE
])->orderBy('title', 'ASC');
# with relations
if($useWith == true)
$Self = $Self->whereDoesntHave('subscriberDevice', function($q) use ($subscriber) {
if(!is_null($subscriber))
$q->where('subscriber_id', '!=', $subscriber->subscriber_id);
});
# building query
$Self = $Self->get();
return ($Self->isEmpty() == false)
? $Self->toArray()
: null;
}
订阅者设备:
public function subscriber()
{
return $this->belongsTo(Subscriber::class);
}
public function deviceInventory()
{
return $this->belongsTo(DeviceInventory::class);
}
响应(当前):
{
0: "-- Device Inventory --",
1: "A1",
2: "A2",
3: "A3",
4: "A4",
5: "A5",
6: "A6",
}
当前用户有3个inventories
与他A1, A2 & A3
关联。
选择要编辑的 inventory
是 A2
。
A4, A5 & A6
可用inventories
可以选择
因此,预期结果为:
{
0: "-- Device Inventory --",
2: "A2",
4: "A4",
5: "A5",
6: "A6",
}
好吧,我花了一段时间。但是,我解决了这个问题。这是我的最终代码...
public function _dropdown($deviceId, $useWith = null, $subscriber = null)
{
...
# with relations
if($useWith == true)
$Self = $Self->whereDoesntHave('subscriberDevice', function($q) use ($subscriber) {
if(!is_null($subscriber))
$query->where('token', '!=', $subscriber->token);
});
...
}
事实证明,唯一需要更改的是 where
条件的列从 subscriber_id
到 token
。 token
是订阅者正在编辑的 inventory
记录的唯一标识符。
希望这可能是遇到类似问题的人。
我将这三个 table device_companies
、devices
和 device_inventory
与关系连接起来,并在创建和更新客户时加载了 ajax。但是,仅加载由 subscriber_devices
table.
create
部分按要求工作。但是,在更新与所选客户关联的所有 devices
时正在加载。例如:如果 customer
有 ABC -> DEF -> A1,A2,A3
而我正在更新 A2
的详细信息,那么 A1,A3
不得出现在下拉列表中。只有 A2
和 A4,A5,A6,...
应该加载。
表格:
device_companies: -> devices: -> device_inventories:
|-- id |-- id |-- id
|-- title |-- device_company_id |-- device_id
... |-- title |-- serial_number
... ...
subscriber_devices:
|-- id
|-- subscriber_id
|-- device_inventory_id
...
设备公司:
public function devices()
{
return $this->hasMany(Device::class);
}
public function _dropdown($useWith = null, $subscriber = null)
{
$Self = self::where('status', \Common::STATUS_ACTIVE)->select([
'id',
'title'
])->orderBy('title', 'ASC');
# with relations
if($useWith == true)
$Self = $Self->whereHas('devices.deviceInventory', function($q) use ($subscriber) {
$q->whereDoesntHave('subscriberDevice', function($q) use ($subscriber) {
if(!is_null($subscriber))
$q->where('subscriber_id', '!=', $subscriber->subscriber_id);
});
});
# building query
$Self = $Self->get();
return ($Self->isEmpty() == false)
? $Self->toArray()
: null;
}
设备:
public function deviceCompany()
{
return $this->belongsTo(DeviceCompany::class);
}
public function deviceInventory()
{
return $this
->hasMany(DeviceInventory::class)
->where('status', \Common::STATUS_ACTIVE)
->orderBy('serial_number', 'ASC');
}
public function _dropdown($deviceCompanyId, $useWith = null, $subscriber = null)
{
$Self = self::select([ 'id', 'title' ])->where([
'device_company_id' => $deviceCompanyId,
'status' => \Common::STATUS_ACTIVE
])->orderBy('title', 'ASC');
# with relation
if($useWith == true)
$Self = $Self->whereHas('deviceInventory', function($q) use ($subscriber) {
$q->whereDoesntHave('subscriberDevice', function($q) use ($subscriber) {
if(!is_null($subscriber))
$q->where('subscriber_id', '!=', $subscriber->subscriber_id);
});
});
# building query
$Self = $Self->get();
return ($Self->isEmpty() == false)
? $Self->toArray()
: null;
}
设备清单:
public function subscriberDevice()
{
return $this->hasOne(SubscriberDevice::class);
}
public function _dropdown($deviceId, $useWith = null, $subscriber = null)
{
$Self = self::select([
'id',
'serial_number AS title'
])->where([
'device_id' => $deviceId,
'status' => \Common::STATUS_ACTIVE
])->orderBy('title', 'ASC');
# with relations
if($useWith == true)
$Self = $Self->whereDoesntHave('subscriberDevice', function($q) use ($subscriber) {
if(!is_null($subscriber))
$q->where('subscriber_id', '!=', $subscriber->subscriber_id);
});
# building query
$Self = $Self->get();
return ($Self->isEmpty() == false)
? $Self->toArray()
: null;
}
订阅者设备:
public function subscriber()
{
return $this->belongsTo(Subscriber::class);
}
public function deviceInventory()
{
return $this->belongsTo(DeviceInventory::class);
}
响应(当前):
{
0: "-- Device Inventory --",
1: "A1",
2: "A2",
3: "A3",
4: "A4",
5: "A5",
6: "A6",
}
当前用户有3个inventories
与他A1, A2 & A3
关联。
选择要编辑的 inventory
是 A2
。
A4, A5 & A6
可用inventories
可以选择
因此,预期结果为:
{
0: "-- Device Inventory --",
2: "A2",
4: "A4",
5: "A5",
6: "A6",
}
好吧,我花了一段时间。但是,我解决了这个问题。这是我的最终代码...
public function _dropdown($deviceId, $useWith = null, $subscriber = null)
{
...
# with relations
if($useWith == true)
$Self = $Self->whereDoesntHave('subscriberDevice', function($q) use ($subscriber) {
if(!is_null($subscriber))
$query->where('token', '!=', $subscriber->token);
});
...
}
事实证明,唯一需要更改的是 where
条件的列从 subscriber_id
到 token
。 token
是订阅者正在编辑的 inventory
记录的唯一标识符。
希望这可能是遇到类似问题的人。