我有4张桌子:
合作伙伴 用户 手机 地址
地址,电话和合作伙伴表每个都有一个 fk 到用户表的id,以允许用户 - >所有者对象环境。
电话和地址表每个都有一个 fk 到合作伙伴表中的ID。
partners表有一个primaryPhone和一个primaryAddress字段,用于将id存储到其主要地址和电话号码中。
我希望能够查询与用户关联的所有合作伙伴,同时将有关其主要电话和主要地址的信息绘制到阵列中以显示在网格中。
有没有办法在单个查询中执行此操作?
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的信息输入到partners数组中,以便在视图中我可以进行foreach并显示数据?
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;
答案 0 :(得分:0)
我相信您可以使用预先加载来加载您想要显示的相关模型。文档:http://laravel.com/docs/eloquent#eager-loading
我认为这应该有用,虽然我自己没有测试过。
$partners = Partners::with('primaryPhone')->where('user_id', Auth::user()->id)->get();
答案 1 :(得分:0)
<?php
$partners = Partners::with('phones')->with('adresses')->where('user_id', Auth::user()->id)->get();
//Now you can loop through the partners
foreach($user->partners as $partner)
{
echo $partner->primaryAdress();
echo $partner->primaryPhone();
}
?>