laravel急切装载领域的变化

时间:2019-04-23 17:40:08

标签: php mysql laravel

在我写的用户模型中,我有2个表,“怀旧”和“用户”,它们之间是有联系的。

public function nostalgia(){
    return $this->hasMany(Nostalgia::class);
}

public function getPrivatePhone(){
    return substr_replace($this->phone,"***",4,3);
}

在怀旧模式中:

public function user(){
    return $this->belongsTo(User::class);
}

现在我想在分页模式下获得与用户表有关的所有怀旧信息,以获取用户电话,我想通过我在用户模型中定义的“ getPrivatePhone()”来更改电话号码的某些字符,或者以任何方式。 我怎样才能做到这一点? 我尝试加载时查询此查询,但无法更改电话号码:

Nostalgia::Where("confirm", "=", true)->with('user:id,phone')->Latest()->paginate(6),

1 个答案:

答案 0 :(得分:2)

您可以按以下方式修改 ServerName http://forums.x.org RequestHeader set X-Forwarded-Proto "http" ProxyRequests off <Proxy *> Order deny,allow Allow from all </Proxy> RewriteEngine On RewriteCond %{REQUEST_URI} ^/socket.io [NC] RewriteCond %{QUERY_STRING} transport=websocket [NC] RewriteRule /(.*) ws://127.0.0.1:4567/$1 [P,L] ProxyPass / http://127.0.0.1:4567/ ProxyPassReverse / http://127.0.0.1:4567/ </VirtualHost> 模型:

User

class User extends Model{ protected $appends = ['privatePhone']; protected $visible = ['id', 'phone', 'privatePhone'] public function getPrivatePhoneAttribute() { return substr_replace($this->phone,"***",4,3); } //Remove the method getPrivatePhone() //And keep everything else as it is }

现在,您可以访问一个赞:

$nostalgias = Nostalgia::Where("confirm", true)->with('user')->Latest()->paginate(6);

或在循环中:

$nostalgias->first()->user->privatePhone