Laravel: 客户有多条记录时的更新问题!

问题描述 投票:0回答:1

我有这三张表 device_companies, devices &amp。device_inventory 与关系连接,并在创建和更新客户时用ajax加载。但是,只有可用的数据才会被加载,这些数据由 subscriber_devices 表。

该表。create 部分,其按要求工作。但是,在更新所有的 devices 相关联的客户正在加载。例如:如果一个 customerABC -> DEF -> A1,A2,A3 我正在更新细节 A2 然后 A1,A3 不得出现在下拉菜单中。只有 A2A4,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
...

DeviceCompany:

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;
}

Device:

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;
}

DeviceInventory:

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;
}

SubscriberDevice:

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",
}

当前用户有三个 inventories 与他 A1, A2 & A3. 该 inventory 选择编辑的是 A2. 该 A4, A5 & A6 可获得 inventories 可以选择的。

因此,预期的结果是

{
    0: "-- Device Inventory --",
    2: "A2",
    4: "A4",
    5: "A5",
    6: "A6",
}
laravel record relation multiple
1个回答
0
投票

嗯,它花了我一段时间。但是,我解决了这个问题。这是我的最终代码...

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_idtoken. 该 token 是用户的唯一标识符。inventory 正在编辑的记录。

希望这可能有人有类似的问题。

© www.soinside.com 2019 - 2024. All rights reserved.