/**
  * Create all of the child nodes of a root node.
  *
  * @param array $nodes
  */
 protected function createNodes($nodes)
 {
     // $parent is the array of parent nodes.
     $parent = [];
     foreach ($nodes as $load_record) {
         $match = preg_match('/^\\*+/', $load_record, $matches);
         // If there is no match then we have hit a top level category.
         // Add this to the $result array and reset the $parent array.
         if ($match === 0) {
             $node_name = trim($load_record);
             $root_node = Category::create(['name' => $node_name, 'description' => $node_name]);
             $parent = [0 => $root_node];
             continue;
         }
         // If there is a match then check how many levels deep we are.
         $levels = strlen($matches[0]);
         // Grab the node name
         $node_name = trim(substr($load_record, $levels));
         // Grab the current node at the level of the parent.
         /** @var Category $current */
         $current = $parent[$levels - 1];
         // Add this node as a child of the parent.
         $child_node = $current->children()->create(['name' => $node_name]);
         // Update the description, just for fun
         $child_node->description = $child_node->path;
         $child_node->save();
         // Store this node name as a potential future parent.
         $parent[$levels] = $child_node;
     }
 }
Example #2
0
 /**
  * Get all of the company types (categories).
  *
  * Returns a id => value array, e.g.
  * 31 => Customer
  * Suitable for use in pull-down lists, and for storage as category_id
  * in the foreign key field in the pivot tables.
  *
  * @return array
  */
 public static function getCompanyTypes()
 {
     $categories = Category::where('slug', '=', 'contact-types')->first()->leaves();
     /** @var array $result */
     $result = [];
     /** @var Category $category */
     foreach ($categories as $category) {
         $result[$category->id] = $category->name;
     }
     return $result;
 }
 public function run()
 {
     $nodes = $this->getNodes();
     // Build the above list of nodes as a heirarchical tree
     // of categories.
     foreach ($nodes as $node_name => $node_children) {
         // Create each root node.
         $root_node = Category::create(['name' => $node_name, 'description' => $node_name]);
         // Create the children of the root node.
         if (!empty($node_children)) {
             $this->createNodes($root_node, $node_children);
         }
     }
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     // Example address
     /** @var Address $address_pm */
     $address_pm = Address::create(['street' => '10 Downing Street', 'city' => 'London', 'country_name' => 'United Kingdom']);
     // Find category
     $contact_lead = Category::where('slug', '=', 'lead')->first();
     $company = Company::create(['company_name' => "Prime Minister's Office", 'contact_name' => 'David Cameron', 'website' => 'https://en.wikipedia.org/wiki/Prime_Minister_of_the_United_Kingdom', 'category_id' => $contact_lead->id]);
     $contact = Contact::create(['first_name' => 'David', 'last_name' => 'Cameron', 'sort_order' => 'en', 'company_id' => $company->id, 'position' => 'Prime Minister', 'timezone' => 'Europe/London', 'dob' => '1966-10-09', 'gender' => 'M', 'email' => '*****@*****.**', 'category_id' => $contact_lead->id]);
     // Attachments
     $company->addresses()->attach($address_pm->id, ['address_type' => 'head-office', 'status' => 'current']);
     $contact->addresses()->attach($address_pm->id, ['address_type' => 'office', 'status' => 'current', 'start_date' => '2010-05-11']);
     // Something to demonstrate name sorting
     $address_sa = Address::create(['street' => '163 Uys Krige Drive', 'suburb' => 'Plattekloof', 'city' => 'Cape Town', 'country_name' => 'South Africa']);
     $company_sa = Company::create(['company_name' => 'South African Rugby Union', 'website' => 'http://www.sarugby.net/', 'category_id' => $contact_lead->id]);
     $contact_sa = Contact::create(['first_name' => 'Joost', 'last_name' => 'van der Westhuizen', 'sort_order' => 'nl', 'company_id' => $company_sa->id, 'position' => 'Scrum Half', 'category_id' => $contact_lead->id]);
     $company_sa->addresses()->attach($address_sa->id);
     $contact_sa->addresses()->attach($address_sa->id, ['address_type' => 'office', 'status' => 'previous', 'start_date' => '1993-02-11', 'end_date' => '2003-09-09']);
 }