繁体   English   中英

Laravel 8 belongsTo 关系不返回用户数据 model

[英]Laravel 8 belongsTo relationship not returning data on User model

我正在构建一个 Laravel 8 API 并希望在查询user User时自动将 user_settings 加入user_settings

我的想法是我可以通过belongsTo关系实现这一点,因为user_settings “属于”用户。

但是,当我将其附加到我的UserSetting model 并查询用户时,尽管user_settings表中有数据,但我没有看到任何用户设置附加到我的用户。

我哪里错了?

Model: User

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserSetting extends Model
{
    use HasFactory, SoftDeletes;

    /**
    * The table associated with the model.
    *
    * @var string
    */
    protected $table = 'user_settings';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id',
        'theme',
        'refreshButtonPlacement',
        'animationSpeed',
        'fetchTimeout'
    ];

    /**
     * Get the user that owns the comment.
     */
    public function user()
    {
        return $this->belongsTo(UserSetting::class);
    }
}

Model: User

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;

use Illuminate\Database\Eloquent\Model;
use Tymon\JWTAuth\Contracts\JWTSubject;


class User extends Authenticatable implements JWTSubject
{
    use HasFactory, Notifiable, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name',
        'last_name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'last_login_at' => 'datetime'
    ];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}

我还尝试使用 一对一关系并在我的User model 上定义了一个settings方法,但是在我运行User::findOrFail(1)->settings;时在 Tinker 中定义了一个设置方法。 我也一无所有。

关系设置:

class User extends Model
{
//some custom stuff
    /**
     * Get the phone associated with the user.
     */
    public function user_setting()
    {
        return $this->hasOne(UserSetting::class);
    }
}
class UserSetting extends Model
{
    //some custom things
    /**
     * Get the user that owns the comment.
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

之后您可以默认使用 eager laoding ,在您的情况下,您必须将$with = ['user_setting']添加到您的User class。

您还可以使用->with()方法,因为您必须使用以下任一方法:

 User::with('user_setting')->find(Auth::id());
//or
Auth::user()->with('organisation')->first()

由于明显的开销,Laravel 不会在每次调用中加载关系值。 因此,您要么定义默认加载的关系,要么必须使用->with()方法来预先加载关系。

将此方法添加到您的User model
您可以通过每个User model 实例上的动态属性$user-> user_setting user_setting 访问用户设置有关更多信息https://laravel.com/docs/8.x/eloquent-relationships#one-to-one

    public function user_setting(){
       return $this->hasOne(UserSetting::class);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM