Thursday, 18 May 2017

Topic 19 - Using Dropdown Helper

In some extend , we might use multidropdown for our input which generally passing array values , type , name and manymore. Heres how we do it in laravel. Starting form create widget and respective helper and then call them using Laravel expression { ! Form::classhelper !! }

Widget Layout 

<select name = {{$name}} {{ $type }} class="{!! $class !!} select2" {!! $extra !!} style="width:100%">

    @if(is_array($option))
        @foreach($option as $id => $key)
            @if(is_array($key))
                <optgroup label="{{ $id }}">
                    @foreach($key as $optid => $opt)
                        <option value="{{ $optid }}" {!! (!empty($value) && in_array($optid, $value)) ? 'selected' : '' !!}>{{ $opt }}</option>
                    @endforeach
                </optgroup>
            @else
                <option value="{{ $id }}" {!! (!empty($value) && in_array($id, $value)) ? 'selected' : '' !!}>{{ $key }}</option>
            @endif
        @endforeach
    @else
        <option value=""></option>
        @foreach($option as $option)
            <option value="{{ $option->id }}" {!! (!empty($value) && in_array($option->id, $value)) ? 'selected' : '' !!}>{{ $option->name }}</option>
        @endforeach
    @endif
</select>

Class Helper

public static function multiDropdown($name = 'dropdown', $option = array(), $value = array(), $additional = ''){
        $classmsg = "form-control";
        $additional = Forms::additional($additional, $classmsg);
        $data = array(
            'name' => $name,
            'option' => $option,
            'value' => (!empty($value) && !is_array($value)) ? array($value) : $value,
            'extra' => $additional['extra'],
            'class' => $additional['class'],
            'type' => 'multiple'
        );

        return view('widget.form_dropdown')->with($data);
    }



Form View

<div class="form-group">
    <label class="col-lg-2 control-label">Course</label>
    <div class="col-lg-10">
        {!! Form::MultiDropdown('course[]',$course,'') !!}
    </div>
  </div>


Controller 

public function store(Request $request)
    {
      $catalogue = new Catalogue;
      $purchase = date('Y-m-d', strtotime($request["date_purchase"]));
      $request["date_purchase"] = $purchase;
      // $catalogueid = $request['catalogueid'];
      //
      $request['subject'] = implode(',',$request->input('subject'));
      $request['course'] = implode(',',$request->input('course'));
      $catalogue->insert($request->except('_token'));


      \Session::flash('alert-success', 'Cataloge Successfully Added');
      return redirect(route('catalogue.index'));
    }

Accessing rules ( 3 method ) 

Method 1 - By Controller 

  public function edit($id)
    {
      $typeselection = array(
        '' => '',
        '1' => 'Book',
        '2' => 'Journal',
        '3' => 'Dissertation'
      );

     $data = array(
            'breadcrumb' => array('Library', 'Catalogue','Update'),
            'content' => 'library.catalogue.form',
            'title' => 'Update Catalogoue',
            'type_selection' => $typeselection,
            'type' => 2,
        );
      return view('layouts.template')->with($data);
    }
then ,  call by writing this :

{{$item_status[$catalogue->item_status]}}


Method 2 - By Model

  public function getStatusNameAttribute()
    {
      if ($this->item_status == '1') {
        return 'On Shelf';
      }
      else if ($this->item_status == '2') {
        return 'Lost';
      }
      else if ($this->item_status == '3') {
        return 'Damage';
      }
    }

then ,  call by writing this :

{{$catalogue->StatusName}}

Method 3 - By Repository



No comments:

Post a Comment