我有两个模型:用户和约会
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',
];
}我在约会模式中的关系
public function user()
{
return $this->belongsToMany(User::class);
}user_id和business_id对应于用户模型上的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();
}但我有两次相同的名字
发布于 2022-10-11 09:32:27
假设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关系
发布于 2022-10-11 09:16:31
例如,如果您想要获取医生,则必须使用user__type筛选来获取。
//something like this
$x = $appointments->user()->where('user_type', 'doctor')->first();
$x->name;发布于 2022-10-11 08:51:33
只需在模型中定义关系,如:
在约会模式中写:
public function user ()
{
return $this->your-relationship(User::class);
}供用户使用:
public function appointment ()
{
return $this->your-relationship(Appointment::class);
}https://stackoverflow.com/questions/74025410
复制相似问题