Laravel 3 - 如何从has_many()返回特定字段

时间:2014-02-11 20:34:09

标签: php json eloquent laravel-3

我正试图找出做某事的最佳方法。我有一个表“群组”与另一个表“车辆”(车辆组)的has_many关系。我想查询groups表并返回一组组,每组包含一组相关的车辆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)我希望vehicles属性只包含一个ID数组 - 而不是一堆嵌套对象。

现在,我知道可以在迭代Eloquent对象时解析vehicle属性并格式化查询结果:

$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对象吗?

2 个答案:

答案 0 :(得分:0)

只需使用连接 - 更灵活。

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循环运行它就可以按照你想要的方式运行它。

答案 1 :(得分:0)

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

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() );