2
I have the following file .json
, in case I want to access initial data, called suzuki
at the beginning of json, how do I get this information?
That one .json
is responsible for making the data Seed in the database, and that field is that of the relationship, I just need to pick it up in the class.
The rest of the other data I can take the value and save in the bank.
- Method of registration to Concessionaire.
private function fillDealerships(array $files)
{
foreach ($files as $file) {
$data = $this->getFileContent($file);
foreach ($data as $item) {
$slug = isset($data['u']) ? Str::slug($item['slug']) : $file['filename'];
/* @var $dealership Dealership */
$dealership = Dealership::query()->firstOrCreate([
'slug' => $slug,
$dealership->brands()
], [
'name' => $item['n'] ?? $slug,
]);
$this->fillDealershipStore($dealership, $item);
}
}
}
- Json of the Dealership Stores.
{
"suzuki": [
{
"n": "MOTO MAIS",
"u": "moto-mais",
"e": {
"l": "Av. Oswaldo Da Silva",
"n": "710",
"m": "",
"b": "Conjunto Habitacional Ana Jacinta",
"u": "19064070",
"c": {
"i": "9286",
"n": "Presidente Prudente"
},
"s": {
"i": "26",
"n": "Sao Paulo"
},
"r": "https:\/\/goo.gl\/maps\/aYoJgneRsTo",
"g": [
"-22.1505632",
"-51.4526682"
]
},
"c": {
"t": {
"c": {
"n": "(18) 3901-1911",
"l": "1839011911"
}
}
},
"r": {},
"g": null,
"t": "concessionaria-autorizada"
}
}
private function getFileContent(array $file)
{
return json_decode(file_get_contents($file['path']), true)[$file['filename']];
}
Here he returned 0
– Romulo Sousa
@Romulosousa understood... your json already enters formatted in function... It starts by array. That is, within this function is impossible. 'Cause the key doesn’t exist inside her.
– Andrei Coelho
@Romulosousa how is the array for the function launched? Put the code in the question.
– Andrei Coelho
I added the method to read the file in the question.
– Romulo Sousa
@Romulosousa already know... I will edit.
– Andrei Coelho
Because with this data I make the relationship between the dealership and the brand.
– Romulo Sousa
@Romulosousa edited.
– Andrei Coelho
@Romulosousa worked out?
– Andrei Coelho
It was, but there was an error, in my part, I can not access in its way, that’s right, but because of the loop, being that the name of the file is in
$file['filename
]`. ;array:2 [
 "filename" => "haojue"
 "path" => "C:\wamp64\www\suzuki-cms-backoffice\database\migrations\production\dealerships\haojue.json"
]
– Romulo Sousa
@Romulosousa but the
$file['filename]
is not "Suzuki" ??– Andrei Coelho
@Romulosousa tries the way I edited... in the loop.
– Andrei Coelho
I did so
$filename = $file['filename'];
He returns to me the same as you :D– Romulo Sousa
@Romulosousa returns Suzuki right? I had written wrong! = ) I put an edition.
– Andrei Coelho
Suzuki would be the value that would be caught, all right.
– Romulo Sousa
Now I have a problem taking this data that serves to link the relationship between the dealership and brand
– Romulo Sousa
Let’s go continue this discussion in chat.
– Romulo Sousa