array_key_exists():第一个参数应该是字符串或整数

时间:2017-04-12 04:03:05

标签: php laravel laravel-5

我在项目中使用laravel,我需要从记录的id created_at->hourcreated_at->minute生成no_track。我可以将数据保存到数据库,但是在创建no_track之后我无法将数据保存到数据库中..错误消息说:

  

array_key_exists():第一个参数应该是字符串或   整数。

我附上了错误信息的图片。当我将数据保存到数据库时,我希望从记录的no_track获得id,小时和分钟,并将no_track保存到no_track字段。

I got this error messages

这是我的表单代码:

<div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading" style="background-color: #2a3b48; color: #ffffff"><center>Entri Data Gangguan</center></div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ route('lapor.submit') }}">

                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('id_jenis') ? ' has-error' : '' }}">
                            <label for="id_jenis" class="col-md-4 control-label">Jenis Gangguan</label>

                            <div class="col-md-6">
                                <select name="id_jenis" id="id_jenis" class="form-control jgangguan" value="{{ old('id_jenis') }}" required>
                                    <option disabled="true" selected="true">- Pilihan -</option>
                                @forelse($jgangguan as $jgangguan)
                                        <option value="{{ $jgangguan->id }}">{{ $jgangguan->nama_layanan }}</option>
                                    @empty

                                    @endforelse
                                </select>
                                @if ($errors->has('id_jenis'))
                                    <span class="help-block">
                                    <strong>{{ $errors->first('id_jenis') }}</strong>
                                </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('id_app_jar') ? ' has-error' : '' }} cgangguan">
                            <label for="id_app_jar" class="col-md-4 control-label"></label>

                            <div class="col-md-6">
                                <select name="id_app_jar" id="id_app_jar" class="form-control jappjar" value="{{ old('id_app_jar') }}" required>
                                    <option value="0" disabled selected>- Pilihan -</option>
                                </select>

                                @if ($errors->has('id_app_jar'))
                                    <span class="help-block">
                                    <strong>{{ $errors->first('id_app_jar') }}</strong>
                                </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('detail') ? ' has-error' : '' }}">
                            <label for="detail" class="col-md-4 control-label"></label>

                            <div class="col-md-6">
                                <textarea name="detail" id="detail" class="form-control" value="{{ old('detail') }}" placeholder="Detail Gangguan">

                                </textarea>

                                @if ($errors->has('detail'))
                                    <span class="help-block">
                                    <strong>{{ $errors->first('detail') }}</strong>
                                </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-danger col-md-3">
                                    Lapor
                                </button>
                            </div>
                        </div>
                    </form>

                    <script src="{{ url('/assets/vendor/jquery/jquery.min.js') }}"></script>
                    <script type="text/javascript">
                        $(document).ready(function () {
                            $(document).on('change','.jgangguan', function () {
                                //console.log('its change');

                                var id_jenis=$(this).val();
                                //console.log(id_jenis);
                                var div=$(this).parents();
                                var op="";

                                switch (id_jenis){
                                    case '1' : $.ajax({
                                                    type:'get',
                                                    url:'{!! URL::to('data-jar') !!}',
                                                    data:{'id':id_jenis},
                                                    success:function (data) {
                                                        //console.log('success');
                                                        //console.log(data);
                                                        //console.log(data);
                                                        op+='<option value="0" selected disabled>- Pilih Jaringan -</option>';
                                                        for(i=0;i<data.length;i++){
                                                            op+='<option value="'+data[i].id+'">'+data[i].nama_jaringan+'</option>';
                                                        }
                                                        div.find('.jappjar').html("");
                                                        div.find('.jappjar').append(op);

                                                    },
                                                    error:function () {

                                                    }
                                                });
                                    break;
                                    case '2' : $.ajax({
                                                    type:'get',
                                                    url:'{!! URL::to('data-app') !!}',
                                                    data:{'id':id_jenis},
                                                    success:function (data) {
                                                        //console.log('success');
                                                        //console.log(data);
                                                        //console.log(data);
                                                        op+='<option value="0" selected disabled>- Pilih Aplikasi -</option>';
                                                        for(i=0;i<data.length;i++){
                                                            op+='<option value="'+data[i].id+'">'+data[i].nama+'</option>';
                                                        }
                                                        div.find('.jappjar').html("");
                                                        div.find('.jappjar').append(op);

                                                    },
                                                    error:function () {

                                                    }
                                                });
                                    break;
                                }

                            });
                        });
                    </script>

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

这是我的控制器:

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    $jgangguan = JenisLayanan::all();
    $app = Aplikasi::all();
    $jar = Jaringan::all();
    return view('lapor.create', compact('jgangguan','app','jar'));
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $this->validate($request,[
        'id_jenis' => 'required',
        'id_app_jar' =>'required'
    ]);

    //$status = Status::all();
    $status = 1;
    $codeH = substr(strftime("%H", time()),0);
    $codeM = substr(strftime("%M", time()),0);
    $codeS = substr(strftime("%S", time()),0);
    $id = Auth::user()->id;
    $id_app_jar = $request->id_app_jar;
    $id_jenis = $request->id_jenis;

    $gangguan = new Gangguan();
    $gangguan->id_pelapor = $id;
    $gangguan->id_jenis = $id_jenis;
    $gangguan->id_aplikasi_atau_jaringan = $id_app_jar;
    $gangguan->detail = $request->detail;
    $gangguan->id_status = $status;
    $gangguan->no_track = $codeH.$codeM;
    $gangguan->save();

    $id = $gangguan->id;
    $jam = $gangguan->created_at->hour;
    $menit = $gangguan->created_at->minute;
    $track = $id.$jam.$menit;
    $gangguan = Gangguan::findOrFail($id);
    $gangguan->update(array(
        $gangguan->no_track = $track
    ));

    return redirect()
        ->route('dashboard')
        ->with('message',$track);
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    //
}

public function dataJar(){
    $jar = Jaringan::all();
    return response()->json($jar);
}
public function dataApp(){
    $app = Aplikasi::all();
    return response()->json($app);
}

这是我的表结构:

table structure

2 个答案:

答案 0 :(得分:0)

在我的情况下,此行中有错误:

    $attrs = Attribute::get();
    $attribute_ids = $attrs->map->only(['id']); // solved

代替

    $attrs = Attribute::get();
    $attribute_ids = $attrs->get(['id']); // here was my error

我先替换

第二个无效

这是发生的错误

"message": "array_key_exists(): The first argument should be either a string or an integer",
"exception": "ErrorException",

答案 1 :(得分:0)

就我而言,代码中没有错误,然后我重新启动了本地服务器,但没有任何反应 我清除了缓存,视图,重新运行服务器,作曲家转储自动加载,但没有任何反应。 解决方案

I terminate the current project server and run another project,
this project is run successfully, and after this I run my current project so it works like a charm.