我有 4 张桌子:
合作伙伴用户电话地址
地址、电话和合作伙伴表每个都有一个指向用户表的 id 的fk,以允许用户->所有者对象环境。
电话和地址表每个都有一个指向合作伙伴表中 id的fk 。
合作伙伴表有一个 primaryPhone 和一个 primaryAddress 字段,该字段将 id 存储到他们的主要地址和电话号码。
我希望能够查询与用户关联的所有合作伙伴,同时将与他们的主要电话和主要地址有关的信息绘制到数组中以显示在网格中。
有没有办法在一个查询中做到这一点?
合作伙伴控制器(PartnersController.php)
public function index()
{
$partners = Partners::where('user_id', Auth::user()->id)->get();
return View::make('partners.showall')
->with('partners', $partners)
->with('title', 'partners')
->with('breadcrumb', 'View Partners');
}
我可以将 $partner->primaryPhone 的信息获取到合作伙伴数组中,以便在视图中执行 foreach 并显示数据吗?
合作伙伴模型 (Partners.php)
public function phone()
{
return $this->hasMany('Phone', 'partner_id')->where('user_id', Auth::user()->id);
}
public function primaryPhone($id)
{
return $this->has('Phone', 'id')->where('user_id', Auth::user()->id);
}
表结构
CREATE TABLE `partners` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`firstName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`lastName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`spouseName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`primaryPhone` int(11) NOT NULL,
`primaryAddress` int(11) NOT NULL,
`primaryEmail` int(11) NOT NULL,
`greeting` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`birthDate` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`spouseBirthDate` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`anniversary` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`notes` text COLLATE utf8_unicode_ci NOT NULL,
`profession` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`spouseProfession` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`pNewsletter` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`eNewsletter` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`partnerStatus` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`photoUrl` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `partners_id_unique` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `phones` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`partner_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`number` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `phones_id_unique` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `addresses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`partner_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`city` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`state` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`country` int(11) NOT NULL,
`postalcode` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`address_1` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`address_2` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `addresses_id_unique` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`firstName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`lastName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`phoneNumber` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `users_id_unique` (`id`),
UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;