php - 使用 laravel 预加载选择特定列不起作用

标签 php sql laravel eloquent

当我尝试返回特定列时,我真的不明白为什么这种预加载不起作用。 此代码完美运行

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

不起作用意味着,'points' 不返回任何值,而第一个正确返回值..

知道它有什么问题吗?

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

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

谢谢..

最佳答案

好的解决了。看来我必须传递 user_id、外键,然后它们才能工作。将其作为答案发布,因为其他人可能会得到帮助:)

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

关于php - 使用 laravel 预加载选择特定列不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47736572/

相关文章:

javascript - sequelizejs,使用RegEx进行查询

sql - JSON 在较低级别上没有数组包装器

angular - 我如何调用 laravel 护照的忘记密码并使用 angular 8 验证 api?

php - Laravel whereHas manyToMany 完全匹配

拉拉维尔 : Invalid datetime format: 1292 Incorrect datetime value: '2021-03-14 02:00:00'

php - 如何让用户在网站上而不是在购物车上选择数量

php - 正则表达式仅匹配前面有空格或什么都没有的特定字符(行首)

php - 在php中从数据库填充多个下拉列表

格式化日期时 NodeJS 小时内的 SQL 查询关闭

php - Paypal PHP REST API - 结账时不显示付款金额