mysql - When i am trying to insert data into table, getting error -
i created page using laravel fetching data database, table. in same page given option insert data database. when inserting data database, showing error "sqlstate[23000]: integrity constraint violation: 1062 duplicate entry '' key 'primary' (sql: insert device
(vehicleid
) values (avs))". working , able insert datas. view page giving below
@extends('app') @section('content') <div class="templatemo-content-wrapper"> <div class="templatemo-content"> <ol class="breadcrumb"> <li><a href="{{ url("/") }}"><font color="green">home</font></a></li> <li class="active">vehicle information</li> </ol> <h1>view/edit vehicle information</h1> <p></p> <div class="row"> <div class="col-md-12"> <div class="table-responsive" style="overflow-x:auto;"> <table id="example" class="table table-striped table-hover table-bordered" bgcolor="#fff8dc"> <h3>select vehicle :</h3> <thead> <tr> <th>vehicle id</th> <th>unique id</th> <th>description</th> <th>equipment type</th> <th>sim phone</th> <th>server id</th> <th>ignition state</th> <th>expecting ack</th> <th>active</th> <th>actions</th> </tr> </thead> <tbody> @foreach($devices $device) <tr> <td>{{ $device->vehicleid }}</td> <td>{{ $device->uniqueid }}</td> <td>{{ $device->description }}</td> <td>{{ $device->equipmenttype }}</td> <td>{{ $device->simphonenumber }}</td> <td></td> <td> @if(@$device->ignitionindex == '0') off @else on @endif </td> <td>{{ $device->expectack }}</td> <td> @if($device->isactive == '1') yes @else no @endif </td> <td> <div class="btn-group"> <button type="button" class="btn btn-info">action</button> <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown"> <span class="caret"></span> <span class="sr-only">toggle dropdown</span> </button> <ul class="dropdown-menu" role="menu"> <li data-toggle="modal" data-target="#acceptmodal" data-bookingid="{{ $device->vehicleid }}"><a href="#">view/ edit</a> </li> <li><a href="{{ url('/vehicle/delete/'.$device->vehicleid)}}">delete</a></li> </ul> </div> </td> </tr> @endforeach </tbody> </table> {{--{!! $results->appends(['sort' => $sort])->render() !!}--}} {{$devices->links()}} </div> </div> </div> </div> </div> {{--{!! $device->links()!!}--}} </br> <h4>create new vehicle</h4> <form role="form" method="post" action="{{ url('vehicleadmin') }}"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="row"> <div class="col-md-6 margin-bottom-15"> <input type="text" class="form-control" name="vehicleid" value="{{ old('vehicleid') }}" placeholder="enter vehicle id"> </div> <div class="row templatemo-form-buttons"> <div class="submit-button"> <button type="submit" class="btn btn-primary">new</button> </div> </div> </div> </form> <script type="text/javascript"> $(document).ready(function() { $('#example').datatable(); } ); </script> @endsection
controller page is
<?php namespace app\http\controllers; use mail; use illuminate\support\facades\db; use app\device; use app\http\requests\createuserrequest; use illuminate\support\facades\auth; use illuminate\support\facades\validator; use illuminate\support\facades\input; use illuminate\pagination\paginator; class vehiclecontroller extends controller { public $type = 'device'; public function getindex() { $devices = db::table('device')->simplepaginate(15); return view('vehicle.vehicleadmin')->with('devices', $devices); } public function vehicleinsert() { $postuser = input::all(); //insert data mysql table $data = array('vehicleid'=> $postuser['vehicleid'] ); // echo print_r($data); $ck = 0; $ck = db::table('device')->insert($data); //echo "record added successfully!"; $devices = db::table('device')->simplepaginate(50); return view('vehicle.vehicleadmin')->with('devices', $devices); } public function delete($id) { db::table('device')->where('vehicleid', '=', $id)->delete(); return redirect('vehicleadmin'); } }
and route page is
route::any('vehicleadmin', 'vehiclecontroller@getindex'); route::post('vehicleadmin', 'vehiclecontroller@vehicleinsert'); route::delete('vehicle/delete/{id}', 'vehiclecontroller@delete');
can 1 tell me why error coming, , need do? in advance
i think vehicleid auto-increment , no need insert
public function vehicleinsert() { $postuser = input::all(); //insert data mysql table $data = array('other-columns'=> $postuser['other-columns'] ); // echo print_r($data); $ck = 0; $ck = db::table('device')->insert($data); //echo "record added successfully!"; $devices = db::table('device')->simplepaginate(50); return view('vehicle.vehicleadmin')->with('devices', $devices); }
Comments
Post a Comment