【问题标题】:Laravel 3 — how to return specific fields from has_many()Laravel 3 — 如何从 has_many() 返回特定字段
【发布时间】:2014-02-11 20:34:09
【问题描述】:

我正在尝试找出做某事的最佳方法。我有一个与另一个表“车辆”(车辆组)有 has_many 关系的表“组”。我想查询组表并返回一组组,每个组包含一组相关的车辆 ID(如果有)。最终结果应该是一个 JSON 对象数组:

[{"id":4534,"group_name":"Annual",vehicles:[2311,3357]},{"id":4752,"group_name":"Summer",vehicles:[5,3348,4316]},{"id":4533,"group_name":"Winter",vehicles:[3116]}];

到目前为止,使用这些方法:

public function vehicle()
{
    return $this->has_many('Vehicle');
}

public static function many()
{
    return self::with (array('vehicle'))
            ->where('account_id', '=', Session::get('account_id_selected'))
            ->order_by('group_name')
            ->select(array('id', 'group_name'))
            ->get();
}

我得到这个结果:

[0] => Array
    (
        [id] => 4534
        [group_name] => Annual
        [vehicle] => Array
            (
                [0] => Array
                    (
                        [id] => 2311
                        [created] => 2007-06-01

                    )

                [1] => Array
                    (
                        [id] => 3357
                        [created] => 2008-08-25
                    )

            )

    )

[1] => Array
    (
        [id] => 4752
        [group_name] => Summer
        [vehicle] => Array
            (
                [0] => Array
                    (
                        [id] => 5
                        [created] => 0000-00-00

                [1] => Array
                    (
                        [id] => 3348
                        [created] => 2008-08-18

                [2] => Array
                    (
                        [id] => 4316
                        [created] => 2011-02-24

            )

    )

[2] => Array
    (
        [id] => 4533
        [group_name] => Winter
        [vehicle] => Array
            (
                [0] => Array
                    (
                        [id] => 3116
                        [created] => 2008-05-15

            )

    )

目前,在查询之后,我使用以下内容将其全部转换为 JSON:

 foreach (Group::many() as $group) {
      $groups[] = $group->to_array();
 }
 var Groups = {{ json_encode($groups); }};

上述方法有两个问题(对我来说):(1)它返回车辆表的所有字段(我只想要ID)。 (2) 我希望车辆属性仅包含一组 ID,而不是一堆嵌套对象。

现在,我知道可以在遍历 Eloquent 对象时解析车辆属性并格式化查询结果:

$groups = array();
foreach (Group::many() as $group) {
    $v = array();
    foreach ($group->vehicle as $vehicle) {
        $v[] = $vehicle->id;
    }
    $groups[] = array('id' => $group->id, 'group_name' => $group->group_name, 'vehicles' => $v);
}
var Groups = {{ json_encode($groups); }};

但我真的认为这应该在模型中完成。我想我要问的是,您认为从这种模型关系到生成的 JSON 的最佳方式是什么?是否可以消除 foreach 循环和额外的解析代码以生成上述更简单的 JSON 对象?

【问题讨论】:

    标签: php json eloquent laravel-3


    【解决方案1】:

    只需使用连接 - 更灵活。

    public static function many()
    {
        return self::join('vehicles', 'vehicles.account_id', '=, 'accounts.id')
                ->where('account_id', '=', Session::get('account_id_selected'))
                ->order_by('group_name')
                ->select(array('id', 'group_name', 'vehicles.id as vehicle_id'))
                ->get();
    }
    

    注意:我不确定您的数据库的表结构,因此不得不假设表名是复数并假设了关键关系,但您应该能够解决。

    然后,您将为每个匹配的 vechicle_id 获得一行或数组元素(即 id 将重复)。只需通过某种 foreach 循环运行它即可获得所需的方式。

    【讨论】:

      【解决方案2】:

      您可以使用闭包来限制选择的内容。

      public static function getManyByAccountId( $account_id )
      {
        return self::with (
            array(
                'vehicle' => function($query) use ($account_id ) {
                   $query->select('id as vehicle_id');
                }
            )
        )->select(array('id', 'group_name'))
         ->order_by('group_name')
         ->where('account_id', '=', $account_id);
      }
      

      然后做一些事情:

      $vehicles = json_encode( Vehicles::getManyByAccountId($account_id)->to_array() );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-09
        • 1970-01-01
        • 1970-01-01
        • 2012-07-03
        • 2015-06-11
        相关资源
        最近更新 更多