在Laravel中返回模型的特定列并转换为Json

时间:2014-05-18 06:45:57

标签: laravel-4 eloquent

有两个表是多对多的关系:

用户:

<?php class User extends Eloquent {

    public function roles()
    {
        return $this->belongsToMany('Role');
    } 
}

作用:

class Role extends Eloquent {

    public function users()
    {
        return $this->belongsToMany('User');
    }

}

我没有问题让用户获得所有字段并将其转换为json:

    $role = Role::find($id)->users()->get()->toArray();        
    var_dump($role);

但是当我尝试只检索用户ID字段时,我遇到了将其转换为json的问题,这是我的代码:(如果我不调用 - &gt; get() - &gt; toArray( ),var_dump没有问题。)

    $role = Role::find($id)->users()->select(array('id'))->get()->toArray(); 
    var_dump($role);

有什么想法吗?感谢

修改

错误消息:

Slim Application Errorbody {margin:0; padding:30px; font:12px / 1.5 Helvetica,Arial,Verdana,sans-serif;} h1 {margin:0; font-size:48px; font-weight:normal; line -height:48px;} strong {display:inline-block; width:65px;}

Slim Application Error

由于以下错误,应用程序无法运行:

详细信息

类型: Illuminate \ Database \ QueryException 代码: 23000 消息: SQLSTATE [23000]:完整性约束违规:1052字段列表中的列'id'不明确(SQL:选择idusers。*,role_userrole_idpivot_role_id,{{1来自role_user上的user_id内部加入pivot_user_id的{​​{1}} {} usersrole_user = users。{{1} } idrole_user = role_1)文件: /Users/Public/Website/cast/vendor/illuminate/database/Illuminate/Database/Connection.php 行: 555

跟踪

#0 /Users/Public/Website/cast/vendor/illuminate/database/Illuminate/Database/Connection.php(283): Illuminate\Database\Connection->run('select user_id, role_useridrole_idu...', Array)
#2 /Users/Public/Website/cast/vendor/illuminate/database/Illuminate/Database/Query/Builder.php(1302): Illuminate\Database\Query\Builder->runSelect()
#3 /Users/Public/Website/cast/vendor/illuminate/database/Illuminate/Database/Query/Builder.php(1289): Illuminate\Database\Query\Builder->getFresh(Array)
#4 /Users/Public/Website/cast/vendor/illuminate/database/Illuminate/Database/Eloquent/Builder.php(416): Illuminate\Database\Query\Builder->get(Array)
#5 /Users/Public/Website/cast/vendor/illuminate/database/Illuminate/Database/Eloquent/Relations/BelongsToMany.php(145): Illuminate\Database\Eloquent\Builder->getModels()
#6 /Users/Public/Website/cast/app/controllers/Api/v1/TestController.php(24): Illuminate\Database\Eloquent\Relations\BelongsToMany->get()
#7 [internal function]: Api\v1\TestController->testRelation('role_1')
#8 /Users/Public/Website/cast/vendor/slim/slim/Slim/Route.php(173): call_user_func_array(Array, Array)
#9 [internal function]: Slim\Route->Slim{closure}('role_1')
#10 /Users/Public/Website/cast/vendor/slim/slim/Slim/Route.php(462): call_user_func_array(Object(Closure), Array)
#11 /Users/Public/Website/cast/vendor/slim/slim/Slim/Slim.php(1326): Slim\Route->dispatch()
#12 /Users/Public/Website/cast/vendor/slim/slim/Slim/Middleware/Flash.php(85): Slim\Slim->call()
#13 /Users/Public/Website/cast/vendor/slim/slim/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call()
#14 /Users/Public/Website/cast/vendor/slim/slim/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call()
#15 /Users/Public/Website/cast/vendor/slim/slim/Slim/Slim.php(1271): Slim\Middleware\PrettyExceptions->call()
#16 /Users/Public/Website/cast/vendor/illuminate/support/Illuminate/Support/Facades/Facade.php(205): Slim\Slim->run()
#17 /Users/Public/Website/cast/public/index.php(4): Illuminate\Support\Facades\Facade::__callStatic('run', Array)
#18 /Users/Public/Website/cast/public/index.php(4): SlimFacades\App::run()
#19 {main}

编辑2:

错误是由select语句中的字段引起的。我改成了:

id

它现在返回一些值。但不幸的是,它仍然返回所有字段,而不仅仅是'users.id'字段。

2 个答案:

答案 0 :(得分:2)

根据Taylor Otwell的说法,目前无法选择要使用belongsToMany返回的列:https://github.com/laravel/laravel/issues/2679

这个解决方案怎么样:

作用

class Role extends Eloquent {

    public function users()
    {
        return $this->belongsToMany('User')->select('id');
    }

}

然后

$roles = Role::find($id)->with('users')->get();
$users = $roles->users;
$usersId = $users->lists('id');

答案 1 :(得分:1)

以下是基于Antoine Augusti答案的最终工作版本:

Role.php:

<?php

class Role extends Model {

    public function users()
    {
        //return $this->belongsToMany('User');
        return $this->belongsToMany('User')->select('users.id');
    }    
}

我的代码列出了所有用户ID:

    $roles = Role::find($id);
    $users = $roles->users();
    $usersId = $users->lists('users.id');
    var_dump($usersId);

usersId将仅显示数组中的用户ID。但我认为$ users仍然包含所有字段。

无论如何它解决了我的问题,虽然它并不完美。谢谢Antoine Augusti。