为什么hasOne和HasMany在Eloquent中生成相同的Sql查询?

时间:2017-04-15 00:18:55

标签: php sql laravel activerecord eloquent

我有两个型号:

class User extends Eloquent
{
    public function phone()
    {
        return $this->hasOne('App\Models\Phone');
    }

    public function phones()
    {
        return $this->hasMany('App\Models\Phone');
    }
}

class Phone extends Eloquent
{
    public function user()
    {
        return $this->belongsTo('App\Models\User');
    }
}

如果User::first()->phone()->toSql()User::first()->phone()->toSql()生成相同的原始查询:

select * from `phones` where `phones`.`user` = ? and `phones`.`user` is not null

hasOnehasMany关系有什么区别?

0 个答案:

没有答案