首页 > 解决方案 > Laravel 8 belongsTo 关系不返回用户模型上的数据

问题描述

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

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

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

我哪里错了?

模型: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);
    }
}

模型: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 [];
    }
}

我还尝试使用一对一关系并settings在我的模型上定义了一个方法,User但是在 Tinker 中,当我运行时,User::findOrFail(1)->settings;我什么也没有。

标签: phplaraveleloquent

解决方案


关系设置:

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课程中。

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

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

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


推荐阅读