text: Q:
  
  Multi Level Category in Select Option
  
  I have a categories table :
  | cid  | title | slug | parent_id | image |status |
  
  I want to show category list in select option, But i need to add a dash character ('-') before any categories that have a parent category.
  Example as follows:
  Furniture
  -Table
  --Sidetable
  ---Designer Table
  ---Simple Table
  -Sofa
  -Chair
  -Doors
  Controller
  public function edit($id)
  {
      $data=Category::all();
      $item=Category::find($id);
      return view('admin.category.edit',array('item'=>$item,'data'=>$data));
  }
  
  View
  @foreach($data as $category)
  <option value="{{$category->cid}}" @if($category->cid == $item->parent_id) selected="selected" @endif> {{ $category->parent_id ? ' -- ' . $category->name : $category->name }}</option>
  @endforeach
  
  Model
  class Category extends Model
  {
      protected 
  $primaryKey = 'cid';
  protected $table = 'categories';
  
  protected $fillable = 
  array('name','url','description','cover','status','parent_id');
  
  function getRouteKeyName() {
      return 'url';
  }
  
  public function parents()
  {
      return $this->belongsTo('app\Category', 'parent_id');
  }
  
  public function children()
  {
      return $this->hasMany('app\Category', 'parent_id','cid');
  }
  
  }
  
  A:
  
  I think this option will solve the problem (in order to simplify some mandatory checks for empty values are omitted):
  Controller:
  public $categories = [];
  public $optionsList = [];
  
  public function edit($id)
  {
      $this->categories = Category::all()->toArray();
      $item = Category::find($id);
  
      $firstLevelCategories = Arr::where($this->categories, function ($item, $key) {
          return empty($item['parent_id']);
      });
      $this->processCategoriesLevel($firstLevelCategories);
  
      return view('edit', ['item' => $item, 'data' => $this->optionsList]);
  }
  
  private function processCategoriesLevel($categories, $level = 0) {
      $level++;
  
      foreach ($categories as $key => $category) {
          $children = Arr::where($this->categories, function ($value, $key) use ($category) {
              return $category['cid'] == $value ['parent_id'];
          });
          $this->optionsList[] = [
              'cid' => $category['cid'],
              'name' => str_repeat('-', $level - 1) . ' ' . $category['name'],
          ];
  
          $this->processCategoriesLevel($children, $level);
      }
  }
  
  View:
  @foreach($data as $category)
      <option
          value="{{$category['cid']}}"
          @if($category['cid'] == $item->parent_id) selected="selected" @endif
      >
          {{$category['name']}}
      </option>
  @endforeach
  
  