Php Laravel从两列变形多个
我需要找到一种方法来修改morphMany关系以变形两个不同的列。以下是正在变形的表创建语法:Php Laravel从两列变形多个,php,mysql,laravel,polymorphism,Php,Mysql,Laravel,Polymorphism,我需要找到一种方法来修改morphMany关系以变形两个不同的列。以下是正在变形的表创建语法: CREATE TABLE `user_friendships` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `sender_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `sender_id` bigint(20) unsigned NOT NULL, `rec
CREATE TABLE `user_friendships` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`sender_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`sender_id` bigint(20) unsigned NOT NULL,
`recipient_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`recipient_id` bigint(20) unsigned NOT NULL,
`status` tinyint(4) NOT NULL DEFAULT '0',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `user_friendships_sender_type_sender_id_index` (`sender_type`,`sender_id`),
KEY `user_friendships_recipient_type_recipient_id_index` (`recipient_type`,`recipient_id`)
) ENGINE=InnoDB AUTO_INCREMENT=12332 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
发件人和收件人都可以是用户。所以我想根据这两个列中包含用户id的任何列进行变形
这只检查发送者是否是用户,而不是接收者
return $this->morphMany(Friendship::class, 'sender');
来自的查询的输出为:
select * from `user_friendships` where `user_friendships`.`sender_id` = 5971 and `user_friendships`.`sender_id` is not null and `user_friendships`.`sender_type` = "App\\\User"
我们真正想要的是:
select * from `user_friendships` where (`user_friendships`.`sender_id` = 5971 and `user_friendships`.`sender_id` is not null and `user_friendships`.`sender_type` = "App\\\User") OR (`user_friendships`.`recipient_id` = 5971 and `user_friendships`.`recipient_id` is not null and `user_friendships`.`recipient_type` = "App\\\User")
我如何做到这一点?
此composer软件包能够解决此问题。请查看此答案,也许可以帮助您