Laravel""如果我尝试选择单列,则返回空列表

时间:2017-05-15 08:11:30

标签: php mysql json laravel eloquent

这是我的问题:

<button ng-click="addCompteItem()">Add a compte</button>
<div ng-repeat="compteItem in compteItems track by $index">
  <h2>
   <strong>Compte {{$index + 1}}</strong>
  </h2>
  <label>Id</label>
  <input type="text" ng-model="compte[$index].id" />
  <label>Unique reference</label>
  <input type="text" ng-model="compte[$index].uniqueRef" />


  <div ng-repeat="mandat in mandats track by $index>
  <label>Id</label>
  <input ng-model="compte[$parent.$index].mandat[$index].id" />

<div>
<button ng-click="addMandatItem($parent.$index,$index)">Add a mandat for          that compte
</button>
 </div>

这是在mysql中执行的:

$scope.compteItems = [{}];
$scope.addCompteItem = function(){
    $scope.compteItems.push({});
};

$scope.mandats=[];
$scope.addMandat = function(i,j){
        console.log(i);
        console.log(j);
        $scope.mandats.push([]);
        var newMandat = {};
        $scope.mandats[i][j]=newMandat;
};

在mysql的控制台中,它会重新结果:

User::with([
        'roles' => function ($query) {
            $query->select('id AS value', 'display_name AS label');
        }
    ])->with([
        'followers' => function ($query) {
            $query->select('id', 'user_id');
        }
    ])->find($id);

在json响应中它是空的:

select `id`, `user_id` from `followers` where 
`followers`.`followable_id` in (1) and `followers`.`followable_type` = 'user'

如果我省略这一行

+----+---------+
| id | user_id |
+----+---------+
|  4 |       2 |
|  5 |       3 |
+----+---------+

然后json响应包含

...
"followers": [],
...

我尝试过的事情:

  1. $ query-&gt;选择(&#39; id&#39;,&#39; user_id&#39;);
  2. $查询 - &GT;选择(&#39; USER_ID&#39);
  3. $ query-&gt;选择(&#39; id&#39;,&#39; *&#39;); //这将返回
  4. 之前显示的内容

    我需要的是:获取用户ID列表,以便响应包含

    $query->select('id', 'user_id');
    

    我知道,我可以在从数据库中获取并修改结果之后执行 ... "followers" => array:2 [▼ 0 => array:6 [▼ "id" => 4 "followable_id" => 1 "followable_type" => "user" "user_id" => 2 ] 1 => array:6 [▼ "id" => 5 "followable_id" => 1 "followable_type" => "user" "user_id" => 3 ] ] ... ,但为什么这个不起作用?

2 个答案:

答案 0 :(得分:1)

with()函数查询中使用addSelect instaead select,因为当您使用select other selection remove

尝试希望它的帮助

答案 1 :(得分:0)

从闭包中进入with方法,您应该返回Query实例。

此外,如果呈现的MySQL查询与您的预期不符,我认为您的关系和/或MySQL查询存在错误。