Example #1
0
 public function index($drop = false, $id = null)
 {
     $model = $this->model();
     $methods = get_class_methods($model);
     if (in_array('indices', $methods)) {
         $indices = $model->indices();
         if (!empty($indices)) {
             $dbIndices = new self('indices', $this->table);
             if ($drop && is_null($id)) {
                 $dbIndices->drop();
             }
             if (is_null($id)) {
                 $rows = $this->get();
                 foreach ($rows as $row) {
                     $indexRow = $dbIndices->firstOrCreate(['object_id' => $row['id']]);
                     foreach ($indices as $index) {
                         $indexRow->{$index} = $row[$index];
                     }
                     $indexRow->save();
                 }
             } else {
                 $row = $this->findOrFail($id)->toArray();
                 $indexRow = $dbIndices->firstOrCreate(['object_id' => $row['id']]);
                 foreach ($indices as $index) {
                     $indexRow->{$index} = $row[$index];
                 }
                 $indexRow->save();
             }
         }
     }
     return $this;
 }