This question use to make me think what should i use, what will be more efficient? so finally i better to choose ask it one time. it may be kind of stupid.
here is my question
I have a multidimensional array
$guests = [
[
"adult" => 2,
"childAge" => [4, 6, 2],
],
[
"adult" => 3,
"childAge" => [5, 7],
]
];
I have to store this array in db, to do that i have two type of approach
-
simply make a table with column name room_guests and store data using json_encode() method and it is easy. so i think it right way to do that or just a cheat.
$roomGuest = new RoomGuestModel; $roomGuest->room_guest = json_encode($guests); $roomGuest->save();
-
Here i think i just make two table
Table_1 name "room_guest" and it has a column name "adult"
Table_2 name "child_age" and it has columns name "room_guest_id" and "age"
Table
room_guests child_age +-----------+ +-----------------------+ | id| adult | | id|room_guest_id| age | +-----------+ +-----------------------+ | 1 | 2 | | 1 | 1 | 4 | | 2 | 3 | | 2 | 1 | 6 | | | | | 3 | 1 | 2 | | | | | 4 | 2 | 5 | | | | | 5 | 2 | 7 | +-----------+ +-----------------------+
store date using loop
Like
foreach ($guests as $guest) { $roomGuest = new RoomGuestModel; $roomGuest->adult = $guest->adult; $roomGuest->save(); foreach ($guest->childAge as $age) { $childAge = new ChildAgeModel; $childAge->room_guest_id = $roomGuest->id; $childAge->age = $age; $childAge->save(); } }
but i have no idea what is the best approach
if you have better idea to do that. so please share.
Thanks in advance
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2iLGLek
via IFTTT
Aucun commentaire:
Enregistrer un commentaire