[英]How to create a relationship between two eloquent models
我有两个模型:用户和约会
class Appointments extends Model
{
use HasFactory;
protected $fillable = [
'business_id',
'user_id',
'subject',
'description',
'status',
'phone_number',
'start_appointment',
'end_appointment',
];
class User extends Authenticatable
{
protected $fillable = [
'name',
'email',
'password',
'card_id',
'assigned_to',
'user_type',
'password_changed_at',
];
}
我在约会 Model 中的关系:
public function user()
{
return $this->belongsToMany(User::class);
}
user_id 和 business_id 对应于用户 model 上的 user_id。我希望能够做这样的事情:
App\Models\Appointments {#5029
id: 1,
business_id: 24,
status: "in_progress",
subject: "Minima similique aspernatur temporibus corporis libero aut voluptatibus.",
description: "The players all played at once set to work, and very soon had to kneel down on the Duchess's cook.",
phone_number: "205-565-2817",
user_id: 27,
start_appointment: "2022-10-22 18:38:17",
end_appointment: "2022-10-22 19:08:17",
created_at: "2022-10-10 16:39:53",
updated_at: "2022-10-10 16:39:53",
user: App\Models\User {#5050
id: 27,
name: "Susan Bosco",
email: "kemmer.sadye@example.com",
card_id: "6163819953",
email_verified_at: "2022-10-10 11:41:57",
#password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
#two_factor_secret: null,
#two_factor_recovery_codes: null,
#remember_token: "zTnSUsbrEL",
current_team_id: null,
profile_photo_path: null,
created_at: "2022-10-10 11:41:57",
updated_at: "2022-10-10 11:41:57",
assigned_to: null,
doctor_type: null,
user_type: 0,
password_changed_at: "2022-10-10 11:41:57",
deleted_at: null,
+profile_photo_url: "https://ui-avatars.com/api/?name=S+B&color=7F9CF5&background=EBF4FF",
},
App\Models\User {#5059
id: 24,
name: "Dr. Floyd O'Keefe MD",
email: "vupton@example.net",
card_id: "1892324633",
email_verified_at: "2022-10-10 11:41:57",
#password: "$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi",
#two_factor_secret: null,
#two_factor_recovery_codes: null,
#remember_token: "Dxb9oivJ7H",
current_team_id: null,
profile_photo_path: null,
created_at: "2022-10-10 11:41:57",
updated_at: "2022-10-10 11:48:55",
assigned_to: 28,
doctor_type: null,
user_type: 2,
password_changed_at: "2022-10-10 11:41:57",
deleted_at: null,
},
$appointments->client_name;
$appointments->business_name
这是我如何获得约会
public function getAppointmetns(){
$doctors = User::where([['user_type',2],['assigned_to',auth()->id()]])->pluck('id');
return Appointments::with(['user'])->whereIn('business_id',$doctors)->get();
}
但我两次得到相同的名字
例如,如果你想找医生,你必须使用 user__type 过滤来获取。
//something like this
$x = $appointments->user()->where('user_type', 'doctor')->first();
$x->name;
假设 user_id 和 business_id 都代表用户的 id。
在这种情况下:
# App/Models/Appointment.php
class Appointment
{
public function user()
{
return $this->belongsTo(User::class);
}
public function business()
{
return $this->belongsTo(User::class, 'business_id');
}
}
文档: BelongsTo 关系
只需在 model 中定义关系,例如:
在约会 model 中写:
public function user ()
{
return $this->your-relationship(User::class);
}
对于用户:
public function appointment ()
{
return $this->your-relationship(Appointment::class);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.