英文:
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->belongsTo(PageType::class);
}
public function showable()
{
return $this->morphTo();
}
Model : PageType
public function page()
{
return $this->hasMany(Page::class);
}
Then in your respective individual models make relation like below.
Model: Logo
public function show()
{
return $this->morphOne(Page::class, 'showable');
}
Model: Cta
public function show()
{
return $this->morphOne(Page::class, 'showable');
}
And at last fetch all that at once in your desired format with below query in controller.
$data = HomePageType::select('id', 'key', 'name')
->with(['page.showable'])
->get();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论