如何在laravel 5.2中

时间:2016-09-28 05:33:22

标签: php mysql 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 >--}}
                                                {{--@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>--}}

                                                <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
    }

}

}

如何编写函数来存储来自multiselect的数据下拉到我的表中每行中的每个值以及其他值。谁能告诉我该怎么办?以及如何编写“字符串到数组”转换和“for循环”插入

1 个答案:

答案 0 :(得分: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());
}