Nur Raga
Nur Raga

Reputation: 1

Error "array_key_exists(): The first argument should be either a string or an integer"

I'm using Laravel for my project, and I need to generate no_track from the id of the record, created_at->hour and created_at->minute. I can save the data to the database, but I can't save the data to the database after creating the no_track. The error messages said:

array_key_exists(): The first argument should be either a string or an integer.

I attached an image of the error message. When I save the data to the database I want to get no_track from the id of the record, hour and minute and save the no_track to the no_track field.

I got this error messages

This my form code:

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

This my controller:

/**
 * 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);
}

This my table structure:

Table structure

Upvotes: 0

Views: 8066

Answers (2)

Hadayat Niazi
Hadayat Niazi

Reputation: 2480

In my case there is no error in the code, then i restart my local server but nothing happen I cleared my cache,views,rerun server, composer dump-autoload but nothing should work. Solution

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.

Upvotes: 0

saber tabatabaee yazdi
saber tabatabaee yazdi

Reputation: 4959

In my case error is in this line:

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

instead of

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

i replace first

and the second not worked

this is the error occurred

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

Upvotes: 1

Related Questions