簡體   English   中英

如何連接3個表?

[英]How to connect 3 table?

就我而言,我有三個表:

  • type

    • ID
    • 類型
    • 原因_id
  • reason

    • ID
    • 原因
  • permission

    • ID
    • type_id
    • reason_id(不確定有沒有)

我希望:

{
    "message": "Get all permissions with type and reason",
    "data": [
        {
            "id": "1",
            "types": [
                "id": "1",
                "type": "Sick",
                "reasons": [
                    "id": "1",
                    "reason": "covid-19"
                ]
            ]
        }

在 laravel 試試這個:

Reason::select(
    'reasons.id as reasonId',
    'reasons.reason as reason',
    'types.id as typeId',
    'types.type as type',
    'permissions.id as permissionId',
)
    ->join('types', 'reasons.id', '=', 'types.reason_id')
    ->join('permissions', 'types.id', '=', 'permissions.type_id')
    ->get();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM