Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用laravel-eager加载选择特定列不起作用_Php_Sql_Laravel_Eloquent - Fatal编程技术网

Php 使用laravel-eager加载选择特定列不起作用

Php 使用laravel-eager加载选择特定列不起作用,php,sql,laravel,eloquent,Php,Sql,Laravel,Eloquent,当我试图返回特定的列时,我真的不明白为什么这种急切的加载不起作用。 这段代码工作得很好 $user=User::where('userName', $userName) ->with('points') ->with('examstaken') ->with('question') /*->with(array('points'=>function($query){ $q

当我试图返回特定的列时,我真的不明白为什么这种急切的加载不起作用。 这段代码工作得很好

$user=User::where('userName', $userName)
        ->with('points')
        ->with('examstaken')
        ->with('question')
        /*->with(array('points'=>function($query){
                $query->select('id','points');
         }))*/
         ->get();
这个密码不正确

$user=User::where('userName', $userName)
        /*->with('points')*/
        ->with('examstaken')
        ->with('question')
        ->with(array('points'=>function($query){
                $query->select('id','points');
         }))
         ->get();
不起作用意味着,“点”不返回任何值,而第一个点正确返回值

知道有什么问题吗

这是我的用户模型中的关系

  public function points()
{
    return $this->hasMany('App\Points','user_id');
}

谢谢

好的,解决了。似乎我必须传递用户id和外键,然后他们才能工作。将其作为答案发布,因为其他人可能会获得帮助:

$user=User::where('userName', $userName)
        /*->with('points')*/
        ->with('examstaken')
        ->with('question')
        ->with(array('points'=>function($query){
                $query->select('user_id','points');
         }))
         ->get();

好的,解决了。似乎我必须传递用户id和外键,然后他们才能工作。将其作为答案发布,因为其他人可能会获得帮助:

$user=User::where('userName', $userName)
        /*->with('points')*/
        ->with('examstaken')
        ->with('question')
        ->with(array('points'=>function($query){
                $query->select('user_id','points');
         }))
         ->get();

如果您没有选择关系所需的键,Eloquent将无法按键将子项与其父母匹配如果您没有选择关系所需的键,Eloquent将无法按键将子项与其父母匹配