Laravel Seed, relating models from a JSON

Asked

Viewed 59 times

1

I’m doing a Seed from a Json file, I can read and populate the data, but at the time of linking the relationship between Dealership and Brands I can’t. How would I do that?

With the $filename = $file['filename']; = Brand name

private function fillDealerships(array $files)
{
    foreach ($files as $file) {
        $data = $this->getFileContent($file);

        foreach ($data as $item) {
            $slug = isset($item['u']) ? Str::slug($item['u']) : $file['filename'];
            $filename = $file['filename'];

            /* @var $dealership Dealership */
            $dealership = Dealership::query()->updateOrCreate([
                'slug' => $slug,
            ], [
                'name' => $item['n'] ?? $slug,
            ]);

            // Brands()
            if (isset($filename)) {
                $dealership->brands();
            }

            $this->fillDealershipStore($dealership, $relation = null, $item);
        }
    }
}

In this section, I need to do the Sync of when I run the method it takes the first item, link with the mark and dps saves and rotate another seeing the loop...

 // Brands()
            if (isset($filename)) {
                $dealership->brands();
            }

  • Structure of the Json
{
  "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"
    },
    {
      "n": "CLEIDSON MOTOS",
      "u": "cleidson-motos",
      "e": {
        "l": "Av. Buriti Grande",
        "n": "458",
        "m": "",
        "b": "Centro",
        "u": "63.210-000",
        "c": {
          "i": "1469",
          "n": "Mauriti"
        },
        "s": {
          "i": "6",
          "n": "Ceara"
        },
        "g": [
          "-7.3873403",
          "-38.7798318"
        ]
      },
      "c": {
        "s": "https:\/\/suzukimotos.com.br",
        "w": {
          "n": "(88) 99682-7914",
          "l": "88996827914"
        },
        "t": {
          "c": {
            "n": "(88) 3552-2114",
            "l": "8835522114"
          }
        }
      },
      "r": {},
      "g": null,
      "t": "concessionaria-autorizada"
    },
}
  • Put an example of JSON as well to make it easier to analyze and help you...

  • All right, I’ll insert

1 answer

2

    private function fillDealerships(array $files)
    {
        foreach ($files as $file) {
            $data = $this->getFileContent($file);

            $filename = $file['filename'];
            /** @var $brand Brand  */
            $brand = Brand::query()->where('slug', $filename)->firstOrFail();

            foreach ($data as $item) {

                \Illuminate\Support\Facades\DB::transaction(function () use ($brand, $item) {
                    $slug = Str::slug($item['u']);

                    /* @var $dealership Dealership */
                    $dealership = Dealership::query()->firstOrCreate([
                        'slug' => $slug,
                    ], [
                        'name' => $item['n'] ?? $slug,
                    ]);

                    /* @var $relation BrandDealerships */
                    $relation = $dealership->dealershipBrands()->firstOrCreate([
                        'brand_id' => $brand->id
                    ]);

                    $this->fillDealershipStore($dealership, $relation, $item);
                });

                gc_collect_cycles();
            }
        }
    }
    ```

  • Explain a little what you did... Don’t just put the code. =)

  • Dealership::query() not only need Dealership::where and so on!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.