在 Laravel 11 中的另一个多对多关系的数据透视表中获取一对多关系

问题描述 投票:0回答:1

我有一个

Place
User
模型,它们与名为
Reservation
的数据透视表/模型具有多对多关系,我需要在数据透视表和另一个名为
reservation_statuses
的表之间建立新的关系来链接每个数据透视表/模型。预订及其状态以及
reservation_statuses
reservation_status_translations
之间的新关系,以多种语言存储预订状态。

现在我需要获取所有登录的用户预订及其预订状态及其翻译,这是我的模型/表结构:

class Place extends Model
{
    use HasFactory, SoftDeletes;

    public function users()
    {
        return $this->belongsToMany(User::class, 'reservations')
            ->withPivot(['id', 'reservation_number', 'reservation_status_id'])
            ->as('reservations')
            ->withTimestamps();
    }
}
class User extends Authenticatable
{
    use HasFactory, HasApiTokens, Notifiable, SoftDeletes, HasRoles;

    public function places()
    {
        return $this->belongsToMany(Place::class, 'reservations')
            ->withPivot(['id', 'reservation_number', 'reservation_status_id'])
            ->as('reservations')
            ->withTimestamps();
    }
}
class Reservation extends Pivot
{
    use HasFactory, SoftDeletes;

    protected $table = 'reservations';

    protected $fillable = [
        'user_id',
        'place_id',
        'reservation_status_id'
    ];

    public function reservationStatus()
    {
        return $this->belongsTo(ReservationStatus::class);
    }
}
class ReservationStatus extends Model
{
    public function reservations()
    {
        return $this->hasMany(Reservation::class);
    }

    public function reservationStatusTranslations()
    {
        return $this->hasMany(ReservationStatusTranslation::class);
    }
}
class ReservationStatusTranslation extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'reservation_status_id',
        'language_id',
    ];
    
    public function reservationStatus()
    {
        return $this->belongsTo(ReservationStatus::class);
    }

    public function language()
    {
        return $this->belongsTo(Language::class);
    }
}

现在我正在尝试获取经过身份验证的用户预订:

public function index()
{
    $user = request()->user();
    $reservations = $user->places()->get(['places.id', 'name']);
    return $this->sendResponse(__('general.list_all_reservations'), $reservations);
}

这是我得到的:

{
    "success": true,
    "message": "List all reservations",
    "data": [
        {
            "id": 1,
            "name": "farm",
            "reservations": {
                "user_id": 151,
                "place_id": 1,
                "id": 122,
                "reservation_number": "772-826-152",
                "reservation_status_id": 4, // here I need the reservation status relationship and then access the reservation status translation relationship
                "created_at": "2024-06-25T19:23:16.000000Z",
                "updated_at": "2024-06-25T19:23:54.000000Z"
            }
        }
    ]
}

如有任何帮助,我们将不胜感激...

php laravel relationship laravel-query-builder laravel-11
1个回答
0
投票

经过多次尝试,我找到了解决方案:

基本上,我需要从数据透视表开始查询构建,并设置 where 条件以仅从经过身份验证的用户 ID 获取数据,在这种情况下,我可以调用

with
函数来访问预订状态并仅添加经过身份验证的用户的条件,这里是一个示例:

/**
 * Display a listing of the resource.
 */
public function index()
{
    $user = request()->user('sanctum');

    $reservations = Reservation::with(['reservationStatus.reservationStatusTranslations' => function ($query) {
        $query->select(['name', 'reservation_status_id']);
        $query->where('language_id', $this->language_id);
    }])->where('user_id', $user->id)->get(); // here i'm making sure I get only the reservations that belongs to the current user 

    return $this->sendResponse(__('general.list_all_reservations'), $reservations);
}

希望这对某人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.