php - 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;
我什么也没有。
解决方案
关系设置:
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()
方法。
推荐阅读
- java - 如何使用 JAVA 中的循环打印此数字模式?
- angular - fabricjs angular 4 动态显示fabricjs画布列表
- java - 如何使用 Spring Boot 使用 API 从 Postman 发布数据表单
- vba - 仅数字文本框不适用于用户表单中的两个文本框之一
- python - 无法在 python 子进程中使用 gcloud compute ssh 命令
- java-8 - 收集时流非法异常
- android - android中的自定义日历?
- oracle-cloud-infrastructure - 线程“主”java.lang.NoSuchFieldError 中的异常:id_ecPublicKey
- java - 具有自动装配到不同参数的构造函数的实现的设计模式
- python - 如何更新保存的映射而不将其加载到内存中?