I'm new to laravel and have to define relationships for 3 tables. The tables are events, categories, packages.
Event can have multiple categories and each category can have multiple packages. Same categories can be in various events.
So far i have following tables but not sure if i'm taking the right approach:
Table:events
id - int
title - varchar ...
Table: categories
id - int
name - varchar
Table:packages
id - int
name - varchar
price - decimal
Table:category_package
category_id - int
package_id - int
Table:event_package
event_id - int
package - id int
Inside package model i have:
public function categories()
{
return $this->belongsToMany('\App\Category');
}
public function events()
{
return $this->belongsToMany('\App\Event');
}
Inside event and category model i have:
public function packages()
{
return $this->belongsToMany('\App\Package');
}
I'd like to get the following result out when i query for specific event with its relationships:
[
"events" => [
"id" => 1,
"title" => "whatever title",
"categories" => [
[
"id" => 1,
"name" => "the category",
"packages" => [
["id" => 1, "name" => "pack1"],
["id" => 2, "name" => "pack2"],
["id" => 3, "name" => "pack3"]
]],
[
"id" => 2,
"name" => "second cat",
"packages" => [
["id" => 4, "name" => "pack4"],
["id" => 5, "name" => "pack5"],
["id" => 6, "name" => "pack6"]
]],
]
]
]
Is this possible in laravel and how would i go about it. I'm pretty sure I didn't take correct approach.
Thanks
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1L1nHwj
via IFTTT
Aucun commentaire:
Enregistrer un commentaire