Laravel MySQL 数据库关联

huangapple go评论58阅读模式
英文:

Laravel Mysql Schema Relations

问题

"sections": [
    {
        "section_type": "logos",
        "data": [
            { "id": 1, "image": "adsadfa", "order": 1 },
            { "id": 1, "image": "aadfad", "order": 1 }
        ]
    },
    {
        "section_type": "cta",
        "data": { "id": 1, "image": "adfa" }
    },
    {
        "section_type": "faqs",
        "data": [
            { "id": 1, "faq_que": "data", "order": 1 },
            { "id": 1, "faq_que": "data", "order": 2 }
        ]
    },
    {
        "section_type": "carousel",
        "data": { "id": 1, "image": "adfa" }
    },
    {
        "section_type": "cta",
        "data": { "id": 1, "image": "adfa" }
    }
]
英文:

I have pages table and logos, faqs, carousel, testmonials, cta tables. Each page can have sections of group of logos, group of faqs, single carousel, can have more than one ctas like page builder. How to establish mysql schema relations in Laravel.

I tried Pages - one to many - sections - many to many logos / faqs. But not getting desired api format. I want to achive below api format for a single page. Can any one suggest the DB schema.

    {
      "section_type": "logos",
      "data": [
        { "id": 1, "image": "adsadfa", "order": 1 },
        { "id": 1, "image": "aadfad", "order": 1 }
      ]
    },
    {
      "section_type": "cta",
      "data": { "id": 1, "image": "adfa" }
    },
    {
      "section_type": "faqs",
      "data": [
        { "id": 1, "faq_que": "data", "order": 1 },
        { "id": 1, "faq_que": "data", "order": 2 }
      ]
    },
    {
      "section_type": "carousel",
      "data": { "id": 1, "image": "adfa" }
    },
    {
      "section_type": "cta",
      "data": { "id": 1, "image": "adfa" }
    }
  ]
}


</details>


# 答案1
**得分**: 1

让您的**ctas、testimonials、faqs、carousels和logos**成为像您现在拥有的那样的单独表格,并为显示分组多个数据创建多对多多态关系,具有以下数据库结构:

表格:*page_types*
```plaintext
id | key    | name  | created_at
------------------------------------
1  | logos  | Logos | 2023-05-06 15:30:19
2  | cta    | CTA   | 2023-05-06 15:30:19

表格:pages

id | home_page_type_id | showable_type     | showable_id | created_at
------------------------------------------------------------------------------
1  | 1                 | App\Models\Logo   | 1           | 2023-05-06 15:30:19
2  | 1                 | App\Models\Logo   | 2           | 2023-05-06 15:30:19
3  | 2                 | App\Models\Cta    | 1           | 2023-05-06 15:30:19

然后像这样定义您的关系:

模型:Page

public function type()
{
    return $this->belongsTo(PageType::class);
}

public function showable()
{
    return $this->morphTo();
}

模型:PageType

public function page()
{
    return $this->hasMany(Page::class);
}

然后在各自的个别模型中进行关系定义:

模型:Logo

public function show()
{
    return $this->morphOne(Page::class, 'showable');
}

模型:Cta

public function show()
{
    return $this->morphOne(Page::class, 'showable');
}

最后,在控制器中使用以下查询一次性获取您所需的格式:

$data = HomePageType::select('id', 'key', 'name')
                    ->with(['page.showable'])
                    ->get();

希望这对您有所帮助。

英文:

Let your ctas, testimonials, faqs, carousels, logos be the seperate tables as it is like you have right now, and make many to many polymorphic relationship for showing groupwise multiple data having below DB structure:

Table : page_types

id | key | name  | created_at
------------------------------------
1  |logos| Logos | 2023-05-06 15:30:19
2  |cta  | CTA   | 2023-05-06 15:30:19

Table : pages

id | home_page_type_id | showable_type     | showable_id | created_at
------------------------------------------------------------------------------
1  | 1                 | App\Models\Logo   | 1           | 2023-05-06 15:30:19
2  | 1                 | App\Models\Logo   | 2           | 2023-05-06 15:30:19
3  | 2                 | App\Models\Cta    | 1           | 2023-05-06 15:30:19

and then define your relation like
Model : Page

    public function type()
    {
        return $this-&gt;belongsTo(PageType::class);
    }


    public function showable()
    {
        return $this-&gt;morphTo();
    }

Model : PageType

    public function page()
    {
        return $this-&gt;hasMany(Page::class);
    }

Then in your respective individual models make relation like below.
Model: Logo

    public function show()
    {
        return $this-&gt;morphOne(Page::class, &#39;showable&#39;);
    }

Model: Cta

    public function show()
    {
        return $this-&gt;morphOne(Page::class, &#39;showable&#39;);
    }

And at last fetch all that at once in your desired format with below query in controller.

$data = HomePageType::select(&#39;id&#39;, &#39;key&#39;, &#39;name&#39;)
                            -&gt;with([&#39;page.showable&#39;])
                            -&gt;get();

huangapple
  • 本文由 发表于 2023年5月6日 16:56:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定