2016-09-28 79 views
0

在我的表單中,我創建了多選下拉列表。我需要將我從多選下拉菜單中選擇的數據存儲到數據庫,表名稱設備列表以及從我的表單中取得的一些值。 我的看法頁面如何將多選下拉數據插入laravel 5.2中的數據庫

@extends('app') 

@section('content') 
    <br><br><br><br><br> 
    <div class="templatemo-content-wrapper"> 
     <div class="container"> 
      <ol class="breadcrumb"> 
       <li><a href="{{ url("/") }}"><font color="green">Home</font></a></li> 
       <li class="active">View/Edit Vehicle</li> 
      </ol> 
      <div class="row"> 
       <div class="col-md-8 col-md-offset-2"> 
        <div class="panel panel-success"> 
         <div class="panel-heading">View/Edit Vehicle Information</div> 
         <div class="panel-body"> 
          @if (count($errors) > 0) 
           <div class="alert alert-danger"> 
            <strong>Whoops!</strong> There were some problems with your input.<br><br> 
            <ul> 
             @foreach ($errors->all() as $error) 
              <li>{{ $error }}</li> 
             @endforeach 
            </ul> 
           </div> 
          @endif 

          <form class="form-horizontal" role="form" method="POST" action="{{ url('vehicle/update/') }}"> 
           <input type="hidden" name="_token" value="{{ csrf_token() }}"> 
@foreach($devices as $device) 
           <div class="form-group"> 
            <label class="col-md-4 control-label">Vehicle ID</label> 
            <div class="col-md-6"> 

             <input type="text" class="form-control" name="deviceID" value="{{ ($device->deviceID)}}"> 

            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">Creation date</label> 
            <div class="col-md-6"> 

             <input type="text" class="form-control" name="creationTime" value="{{ date('Y/m/d H:i:s',($device->creationTime))}}"> 

            </div> 
           </div> 

           <!--<div class="form-group"> 
            <label class="col-md-4 control-label">Server ID</label> 
            <div class="col-md-6"> 

             <input type="text" class="form-control" name="userID" value="" placeholder="Enter User ID"> 

            </div> 
           </div> --> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">Unique ID</label> 
            <div class="col-md-6"> 

             <input type="text" class="form-control" name="uniqueID" value="{{ ($device->uniqueID)}}" placeholder="Enter Unique ID"> 

            </div> 
           </div> 


           <div class="form-group"> 
            <label class="col-md-4 control-label">Active</label> 
            <div class="col-md-6"> 
             <select class="form-control" value="{{ ($device->isActive) }}" name="isActive" > 
              <option value="1">Yes</option> 
              <option value="0">No</option> 
             </select> 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">Vehicle Description</label> 
            <div class="col-md-6"> 
             <input type="text" class="form-control" name="description" value="{{ ($device->description) }}" placeholder="Enter the description"> 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">Short Name</label> 
            <div class="col-md-6"> 
             <input type="text" class="form-control" name="displayName" value="{{ ($device->displayName) }}" placeholder="Enter Display Name"> 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">Vehicle ID</label> 
            <div class="col-md-6"> 
             <input type="text" class="form-control" name="vehicleID" value="{{ ($device->vehicleID) }}" placeholder="Enter Vehicle ID"> 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">License Plate</label> 
            <div class="col-md-6"> 
             <input type="text" class="form-control" name="licensePlate" value="{{ ($device->licensePlate) }}" placeholder="Enter license Plate"> 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">License Expiration</label> 
            <div class="col-md-6"> 
             <input type="date" class="form-control" name="licenseExpire" value="{{ ($device->licenseExpire) }}" placeholder="Enter license Expire Date"> 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">Equipment Type</label> 
            <div class="col-md-6"> 
             <input type="email" class="form-control" name="equipmentType" value="{{ ($device->equipmentType) }}" placeholder="Enter E-Mail Address"> 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">Equipment Status</label> 
            <div class="col-md-6"> 
             <select class="form-control" value="{{ ($device->equipmentStatus) }}" name="equipmentStatus" > 
              <option>Unspecified</option> 
              <option value="inservice">In Service</option> 
              <option value="rented">Rented</option> 
              <option value="pending">Pending</option> 
              <option value="completed">Completed</option> 
              <option value="available">Available</option> 
              <option value="unavailable">Unavailable</option> 
              <option value="repair">Repair</option> 
              <option value="retired">Retired</option> 

             </select> 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">IMEI/EDN Number</label> 
            <div class="col-md-6"> 
             <input type="email" class="form-control" name="imeiNumber" value="{{ ($device->imeiNumber) }}" placeholder="Enter IMEI/EDN Number"> 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">Serial Number</label> 
            <div class="col-md-6"> 
             <input type="email" class="form-control" name="serialNumber" value="{{ ($device->serialNumber) }}" placeholder="Enter Serial Number"> 
            </div> 
           </div> 

          <!-- <div class="form-group"> 
            <label class="col-md-4 control-label">Data Key</label> 
            <div class="col-md-6"> 
             <input type="email" class="form-control" name="notifyEmail" value="" placeholder="Enter E-Mail Address"> 
            </div> 
           </div> --> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">SIM Phone</label> 
            <div class="col-md-6"> 
             <input type="email" class="form-control" name="simPhoneNumber" value="{{ ($device->simPhoneNumber) }}" placeholder="Enter SIM Phone Number"> 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">SMS Email Address</label> 
            <div class="col-md-6"> 
             <input type="email" class="form-control" name="smsEmail" value="{{ ($device->smsEmail) }}" placeholder="Enter SMS E-Mail Address"> 
            </div> 
           </div> 

          <!-- <div class="form-group"> 
            <label class="col-md-4 control-label">Group Pushpin ID</label> 
            <div class="col-md-6"> 
             <input type="email" class="form-control" name="notifyEmail" value="" placeholder="Enter E-Mail Address"> 
            </div> 
           </div> --> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">Map Route Color</label> 
            <div class="col-md-6"> 
             <select class="form-control" value="{{ ($device->displayColor) }}" name="displayColor" > 
              <option>Default</option> 
              <option value="#000000">Black</option> 
              <option value="#a52a2a">Brown</option> 
              <option value="#dd0000">Red</option> 
              <option value="#b37400">Orange</option> 
              <option value="#008f00">Green</option> 
              <option value="#0000ee">Blue</option> 
              <option value="#9400d3">Purple</option> 
              <option value="#505050">Grey</option> 
              <option value="#00b3b3">Cyan</option> 
              <option value="#ff1493">Pink</option> 
              <option value="none">None</option> 
             </select> 
            </div> 
           </div> 




           <div class="form-group"> 
            <label class="col-md-4 control-label">Fuel Capacity</label> 
            <div class="col-md-6"> 
             <input type="text" class="form-control" name="fuelCapacity" value="{{ ($device->fuelCapacity) }}" > 
            </div> 
           </div> 


           <div class="form-group"> 
            <label class="col-md-4 control-label">Driver ID</label> 
            <div class="col-md-6"> 
             <input type="email" class="form-control" name="driverID" value="{{ ($device->driverID) }}"> 
            </div> 
           </div> 

         <!--  <div class="form-group"> 
            <label class="col-md-4 control-label">Reported Odometer</label> 
            <div class="col-md-6"> 
             <input type="email" class="form-control" name="notifyEmail" value="" placeholder="Enter E-Mail Address"> 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-4 control-label">Reported Engine Hours</label> 
            <div class="col-md-6"> 
             <input type="email" class="form-control" name="notifyEmail" value="" placeholder="Enter E-Mail Address"> 
            </div> 
           </div> --> 

            <div class="form-group"> 

              <label class="col-md-4 control-label">Group Membership</label> 
              <div class="col-md-6"> 
              {{--<select class="form-control" value="{{ old('groupID') }}" name="groupID" multiple >--}} 
               {{[email protected]($grouplist as $group)--}} 
                {{[email protected] ($group->groupID == old('description'))--}} 
                 {{--<option value="{{ $group->groupID }}" >{{ $group->groupID." ".'['.$group->description.']' }}</option>--}} 
                {{[email protected]}} 
                 {{--<option value="{{ $group->groupID }}" >{{ $group->groupID." ".'['.$group->description.']' }}</option>--}} 
                {{[email protected]}} 
               {{[email protected]}} 
              {{--</select>--}} 

               <script type="text/javascript"> 
                $(".js-example-basic-multiple").select2(); 
               </script> 

               <select class="js-example-basic-multiple js-states form-control" id="id_label_multiple" value="{{ old('groupID[]') }}" name="groupID" multiple="multiple" > 
                @foreach($grouplist as $group) 
                 @if ($group->groupID == old('description')) 
                  <option value="{{ $group->groupID }}" >{{ $group->groupID." ".'['.$group->description.']' }}</option> 
                 @else 
                  <option value="{{ $group->groupID }}" >{{ $group->groupID." ".'['.$group->description.']' }}</option> 
                 @endif 
                @endforeach 
               </select> 





              </div> 
             </div> 




           <div class="form-group"> 
            <div class="col-md-6 col-md-offset-4"> 
             <button type="submit" class="btn btn-warning"> 
              Update 
             </button> 
            </div> 
           </div> 
@endforeach 
          </form> 
         </div> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
@endsection 

和我的控制器頁面

public function update(Request $request) 
{ 
    try { 

     $postUser = Input::all(); 
     //insert data into mysql table 
     $account = Account::select('accountID')->where('accountID', '=', 'gts')->get(); 


     foreach ($account as $acc) { 
      $abc = $acc->accountID; 
     } 

     $data=DB::table('device') 
      ->where("deviceID",$request['deviceID']) 
      ->update(array(
       "uniqueID"=>$request['uniqueID'], 
       "isActive"=>$request['isActive'], 
       "description"=>$request['description'], 
       "displayName"=>$request['displayName'], 
       "vehicleID"=>$request['vehicleID'], 
       "licensePlate"=>$request['licensePlate'], 
       "licenseExpire"=>$request['licenseExpire'], 
       "equipmentType"=>$request['equipmentType'], 
       "equipmentStatus"=>$request['equipmentStatus'], 
       "imeiNumber"=>$request['imeiNumber'], 
       "serialNumber"=>$request['serialNumber'], 
       "simPhoneNumber"=>$request['simPhoneNumber'], 
       "smsEmail"=>$request['smsEmail'], 
       "displayColor"=>$request['displayColor'], 
       "fuelCapacity"=>$request['fuelCapacity'], 
       "driverID"=>$request['driverID'], 
       "lastUpdateTime"=>time())); 


     $data =  array("accountID" => $abc, 
      "deviceID"=> $request['deviceID'], 
      "groupID"=>$request['groupID[]'], 
      "lastUpdateTime"=>time(), 
      "creationTime"=>time()); 

     $ck = 0; 
     $ck = DB::table('devicelist')->Insert($data); 


     $devices = DB::table('device')->simplePaginate(10); 
     return view('vehicle.vehicleAdmin')->with('devices', $devices); 




    } catch (ModelNotFoundException $err) { 
     //Show error page 
    } 

} 

}

我怎麼能寫從多選刪除數據存儲到我的表中的各每個值的函數與其他值一起排。任何人都可以告訴我該怎麼辦?以及如何爲插入寫入「字符串到數組」和「for循環」

+0

你是從多選下拉菜單中獲取數組或字符串嗎? @Rahul Vp –

回答

0

如果您從多選中獲取數組並希望將其插入一行。你應該破滅數組轉換成以逗號分隔的字符串,然後在表中插入

Implode function

$val = implode(",",$request['groupID[]']); 

然後把$ VAL到$ data數組一樣

$data =  array("accountID" => $abc, 
     "deviceID"=> $request['deviceID'], 
     "groupID"=>$val, 
     "lastUpdateTime"=>time(), 
     "creationTime"=>time()); 

如果你正在然後串爆發,

Explode function

$val = explode(",",$request['groupID[]']); 
$count = count($val); 

for($a=0; $a<$count; $a++){ 
$data =  array("accountID" => $abc, 
     "deviceID"=> $request['deviceID'], 
     "groupID"=>$val[$a], 
     "lastUpdateTime"=>time(), 
     "creationTime"=>time()); 
} 
+0

好的。但我需要將每個值與其他值一起插入單獨的行中。這就夠了。你能告訴我該怎麼做。 –

+0

你從多選下拉菜單中得到什麼? –

+0

實際上,我正在爲多個組分配特定車輛。 –

相關問題