Laravel 模型关联时使用where 查询时 无法得到关联的模型数据

Laravel 模型关联时使用where 查询时 无法得到关联的模型数据


User模型中的一对一关联

 // 模型关联 关联签到表
    public function sign()
    {
    
    
        return $this->hasOne(UserSigns::class,'user_id','id');
    }

在使用laravel的模型关联时,在查询时使用了where条件,发现不出现关联模型的数据:

$res = Users::where(['rcid' => $rcid])->first();

发现在查询结果中,并未出现相关的数据,
改成这样就好了

$res = Users::where(['rcid' => $rcid])->with('sign')->first();

猜你喜欢

转载自blog.csdn.net/Z_PTOPONE/article/details/122163307
今日推荐