我正在使用laravel 5.3。我Promotion
属于Device
而Device
属于多个emails
。任何人都可以给我建议。如何进入促销查询连接到具有此促销的设备的所有电子邮件?
目前我正在尝试这样的事情
在促销模式中
public function devicesWithEmails()
{
return $this->belongsTo(Device::class)->with('Emails');
}
获取查询(在促销中)
public function search($data)
{
$perPage = empty($data['perPage']) ? 10 : $data['perPage'];
$sortField = empty($data['sortField']) ? 'id' : $data['sortField'];
$keyword = empty($data['keyword']) ? null : $data['keyword'];
$promotions = Promotion::whereHas('email', function ($query) use ($keyword) {
if ( ! empty($keyword)) {
$query->where('email', 'like', "%{$keyword}%");
}
})
->orWhere('promo_code', 'like', "%{$keyword}%")
->with('devicesWithEmails')
->orderBy($sortField)
->paginate($perPage);
return $promotions->toArray();
}
设备型号
public function emails()
{
return $this->belongsToMany(Email::class, 'emails_devices');
}
电子邮件模型
public function devices()
{
return $this->belongsToMany(Device::class, 'emails_devices');
}
推广模式
public function device()
{
return $this->belongsTo(Device::class);
}
表结构
CREATE TABLE `devices` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
`apptype` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`oid_id` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`lang` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`referrer` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`app_version` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`mobiledeviceid` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
`name` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
`is_paid` TINYINT(1) NULL DEFAULT NULL,
`status` TINYINT(1) NULL DEFAULT NULL,
`is_internal_user` TINYINT(1) NULL DEFAULT NULL,
`paid_date` DATE NULL DEFAULT NULL,
`paid_date_end` DATE NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB;
CREATE TABLE `emails` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
`status` INT(11) NOT NULL DEFAULT '1',
`email` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
`client_type` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
PRIMARY KEY (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB;
CREATE TABLE `emails_devices` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
`email_id` INT(10) UNSIGNED NOT NULL,
`device_id` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `emails_devices_email_id_foreign` (`email_id`),
INDEX `emails_devices_device_id_foreign` (`device_id`),
CONSTRAINT `emails_devices_device_id_foreign` FOREIGN KEY (`device_id`) REFERENCES `devices` (`id`) ON DELETE CASCADE,
CONSTRAINT `emails_devices_email_id_foreign` FOREIGN KEY (`email_id`) REFERENCES `emails` (`id`) ON DELETE CASCADE
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB;
CREATE TABLE `promotions` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
`status` INT(11) NULL DEFAULT NULL,
`batch_name` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`promo_code` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
`validity` TIMESTAMP NULL DEFAULT NULL,
`date` TIMESTAMP NULL DEFAULT NULL,
`device_id` INT(10) UNSIGNED NULL DEFAULT NULL,
`email_id` INT(10) UNSIGNED NULL DEFAULT NULL,
`used_date` DATE NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `promotions_device_id_foreign` (`device_id`),
INDEX `promotions_email_id_foreign` (`email_id`),
INDEX `promo_code_index` (`promo_code`),
CONSTRAINT `promotions_device_id_foreign` FOREIGN KEY (`device_id`) REFERENCES `devices` (`id`) ON DELETE CASCADE,
CONSTRAINT `promotions_email_id_foreign` FOREIGN KEY (`email_id`) REFERENCES `emails` (`id`) ON DELETE CASCADE
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB;
不幸的是,devicesWithEmails总是返回null。
答案 0 :(得分:0)
找到简单的解决方案
只是
public function search($data)
{
$perPage = empty($data['perPage']) ? 10 : $data['perPage'];
$sortField = empty($data['sortField']) ? 'id' : $data['sortField'];
$keyword = empty($data['keyword']) ? null : $data['keyword'];
$promotions = Promotion::whereHas('email', function ($query) use ($keyword) {
if (!empty($keyword)) {
$query->where('email', 'like', "%{$keyword}%");
}
})
->orWhere('promo_code', 'like', "%{$keyword}%")
->with('device.emails')
->orderBy($sortField)
->paginate($perPage);
return $promotions->toArray();
}