J
J
jazzus2018-12-19 04:03:43
Laravel
jazzus, 2018-12-19 04:03:43

How to bulk add Russian cities to the database using Laravel?

Tried to use this bike:

public function cities()
    {
      $cities=
      [
        'Москва',
        'Санкт-Петербург'
      ];

          foreach ($cities as $city) {
            $objCity = new City;
            $objCity->country_id==1;
            $$objCity->name=$city;
            $objCity->save();
          }
    }

Accordingly, according to my assumptions, the cities should have been added to the database when passing through the activating route method. But during the transition I see an error Creating default object from empty value
When I delete the $city variable, I see the value "Moscow", but for some reason Moscow is not added to the database, it says that the value is empty. I'm still quite new to programming. Can you tell me how to fix my script so that it works?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2018-12-19
@jazzus

$objCity->country_id = 1;
and
PS

when passing through the activating route method
https://laravel.com/docs/5.7/seeding

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question