隐藏数据透视表键中的特定列

时间:2015-11-29 16:25:50

标签: laravel eloquent pivot-table

我有2个表格<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <tx:annotation-driven/> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <context:component-scan base-package="intranetwebapp.*" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/view/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/intranetwebapp" /> <property name="username" value="root" /> <property name="password" value="gargamel" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="packagesToScan" value="intranetwebapp.entity" /> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"> </bean> <!-- Tiles configuration --> <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/tiles/tiles-definitions.xml</value> </list> </property> </bean> </beans> abcxyz关系建立在另一个表格上ManyToMany(其数据将作为abc_xyz键返回) 。但是,检索时pivot密钥会返回pivotabc_id。我可以使用方法xyz_id

访问abc_xyz表中的其他列

但是,我想隐藏响应中的withPivot('dummy')abc_id。我怎么做? 我可以使用$ hidden数组隐藏整个xyz_id键,但我想只隐藏特定列而不是整个键。

当前回复

pivot

因此,我只需要{ "abc_uuid": "some uuid", "xyz" : [ { "xyz_uuid": "some uuid", "pivot": { "abc_id": 1, "xyz_id": 1, "dummy" : "dummy value" } }, { "xyz_uuid": "some uuid", "pivot": { "abc_id": 1, "xyz_id": 2, "dummy" : "dummy value" } } ] } 密钥中的dummy,并隐藏pivotabc_id。我怎么做?

1 个答案:

答案 0 :(得分:0)

找到一种粗暴的方法来完成这项工作。在laravel问题中发现此答案现在无法找到链接。但是,它要求我在模型中添加一个方法并取消设置我不想要的键。

public function toArray()
{
    $attributes = $this->attributesToArray();
    $attributes = array_merge($attributes, $this->relationsToArray());
    foreach($attributes['xyz'] as $key => $value) {
        unset($value['pivot']['abc_id']);
        unset($value['pivot']['xyz_id']);
        $attributes['xyz'][$key] = $value;
    }
    return $attributes;
}

这会从我的回复中取消不需要的密钥。我希望laravel为此提供一个简单的方法。