我有 4 张桌子:
合作伙伴用户电话地址
用户登录后,我想确保在查询中只返回与他的 (id) 关联的记录。
因此,当我查询合作伙伴时,我不仅要返回合作伙伴匹配 (id) 的数据,还要确保用户 (id) 与合作伙伴中的 fk (user_id) 匹配。
现在,对于更复杂的级别,我想知道我是否可以查询合作伙伴(匹配 id 和 user_id)以及从相关的电话和地址表中获取他们的主要电话和主要地址。那么,这是否可能使用 eloquent 和我在下面列出的表格结构?
合作伙伴控制器(PartnersController.php)
public function show($id)
{
$data = Partners::find($id)->where('user_id', Auth::user()->id)->first();
$phone = Partners::find(1)->phone;
//var_dump($phone);
return View::make('partners.showone')
->with('data', $data)
->with('phone', $phone)
->with('title', "$data->firstName $data->lastName")
->with('breadcrumb', "$data->firstName $data->lastName");
}
理想情况下,我只想从 $data 数组中提取与该 primaryPhone 值匹配的电话表中的一条记录。这是否可能同时确保 user_id 与 user()->id 匹配?
查看(showone.blade.php)
我将如何在视图中呈现这些数据?如果电话是一个数组(就像上面那样),我假设以下内容会起作用
@foreach($phone as $ph)
<option value="{{ $ph->id }}"
@if($ph->id == $data->primaryPhone) {{ "selected" }}
@endif
> {{ $ph->number }} - {{ $ph->description }}</option>
@endforeach
合作伙伴模型 (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;