Example #1
0
 function addFilterInCheckboxGroup($form_field, $label, array $options, array $default = [], $db_field = null)
 {
     if (!$db_field) {
         $db_field = $form_field;
     }
     $this->form->putTbGroupCheckbox($form_field, $options)->setLabel($label);
     $this->form->initValues([$form_field => $default]);
     $values = (array) \Input::get($form_field, $default);
     if (count($values)) {
         $this->model->where(function ($query) use($values, $options, $db_field) {
             $filters = [];
             foreach ($options as $k => $label) {
                 if (in_array($k, $values)) {
                     $filters[] = $db_field . '_' . $k;
                     if (is_array($db_field)) {
                         $condition = Arr::get($db_field, $k);
                         $query->orWhere($condition);
                     } else {
                         $query->orWhere($db_field, '=', $k);
                     }
                 }
             }
             $this->addJsFilter(implode('|', $filters));
         });
     }
 }
 function buildForm()
 {
     $form = new LaraForm($this->relation);
     $form->putTbHidden('relation')->setValue($this->relation);
     $options = [];
     $model = $this->traitEntityBelongsToMany_getBelongsModel();
     if ($model) {
         $options = $model::orderBy($this->orderByModel())->listsExt();
         if (!is_array($options)) {
             $options = [];
         }
     }
     $form->putTbGroupCheckbox('belongs_to_many', $options, false);
     $ids = $this->ids();
     $form->initValues(['belongs_to_many' => array_combine($ids, $ids)]);
     if ($form->validate()) {
         $many = $form->getFieldValue('belongs_to_many');
         $filtered = array_filter($many, function ($value) {
             return $value;
         });
         $ids = array_keys($filtered);
         $this->model->{$this->relation}()->sync($ids);
     }
     $this->traitEntityBelongsToMany_form = $form;
 }
 function getForm()
 {
     $form = new LaraForm($this->relation);
     $form->putTbHidden('relation')->setValue($this->relation);
     $options = $this->getOptions();
     if (!is_array($options)) {
         $options = [];
     }
     $form->putTbGroupCheckbox('has_many', $options, false);
     $ids = $this->ids();
     $form->initValues(['has_many' => array_combine($ids, $ids)]);
     return $form;
 }