Test Laravel Collections

3 years ago

vrusua

Code

<?php


class Tag
{

  const PROJECT_TYPES = [
      'action',
      'base resource',
      'card',
      'field',
      'filter',
      'lens',
      'partition',
      'resource',
      'resource tool',
      'theme',
      'tool',
      'trend',
      'value',
  ];

  const PROJECT_TYPES2 = [
      ['id' => 1, 'name' => 'action'],
      ['id' => 2, 'name' => 'base resource'],
      ['id' => 3, 'name' => 'card'],
      ['id' => 4, 'name' => 'field'],
      ['id' => 5, 'name' => 'filter'],
      ['id' => 6, 'name' => 'lens'],
      ['id' => 7, 'name' => 'partition'],
      ['id' => 8, 'name' => 'resource'],
      ['id' => 9, 'name' => 'resource tool'],
      ['id' => 10, 'name' => 'theme'],
      ['id' => 11, 'name' => 'tool'],
      ['id' => 12, 'name' => 'trend'],
      ['id' => 13, 'name' => 'value'],
  ];

  protected $guarded = ['id'];


  public function packages()
  {
    return $this->belongsToMany(Package::class)->withTimeStamps();
  }

  public function projectTypeSlugs2()
  {
    return collect(self::PROJECT_TYPES2)->mapWithKeys(function ($name) {
        return [$name['id'] => Str::slug($name['name'])];
    })->toArray();
  }

  public function projectTypeSlugs()
  {
    return collect(self::PROJECT_TYPES)->map(function ($name) {
        return Str::slug($name);
    })->toArray();
  }
}


(new(Tag))->projectTypeSlugs2();

Output

=> [
     1 => "action",
     2 => "base-resource",
     3 => "card",
     4 => "field",
     5 => "filter",
     6 => "lens",
     7 => "partition",
     8 => "resource",
     9 => "resource-tool",
     10 => "theme",
     11 => "tool",
     12 => "trend",
     13 => "value",
   ]
New course: Desktop apps with Electron