2

I have a checkbox when i selected i have on database the value '1' but when i dont select i have this erreur {SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'actif' cannot be null (SQL: insert into techniciens (user_id, actif, moyenne_avis, updated_at, created_at) values (6, , 30.555, 2018-03-14 09:07:15, 2018-03-14 09:07:15))}

create.blade.php

@extends('Layouts/app')
@extends('Layouts.master')
@section('content')
@if(count($errors))

    <div class="alert alert-danger" role="alert">
    <ul>
      @foreach($errors ->all() as $message)
         <li>{{$message}}</li>
      @endforeach
    </ul>
     </div>
    @endif
    <div class="container">
    <div class="row">
        <div class="col-md-10">
            <h1>Ajouter Technicien</h1>
        <form action=" {{url ('technicien')  }}" method="post">
         {{csrf_field()}}
            <div class="form-group">
                <label for="">Nom</label>
                <input id="nom" type="text" class="form-control" name="nom" 
 value="{{ old('nom')   
            }}" required autofocus>

                            @if ($errors->has('nom'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('nom') }}
 </strong>
                                </span>
                            @endif
            </div>
            <div class="form-group">
                <label for="">Prenom</label>
                <input id="prenom" type="text" class="form-control" 
 name="prenom" value="{{ 
          old('prenom') }}" required autofocus>

                            @if ($errors->has('prenom'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('prenom') }}
 </strong>
                                </span>
                            @endif
            </div>
            <div class="form-group">
                <label for="">Telephone</label>
                <input id="tel" type="text" class="form-control" name="tel" 
value="{{ old('tel') }}" 
        required autofocus>

                            @if ($errors->has('tel'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('tel') }}
 </strong>
                                </span>
                            @endif
            </div>
            <div class="form-group">
                <label for="">Mobile</label>
                <input id="mobil" type="text" class="form-control" 
 name="mobil" value="{{ 
      old('mobil') }}" required autofocus>

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

            <div class="form-group">
                <label for="">Role</label>
                <input id="role" type="text" class="form-control" 
 name="role" value="{{ old('role') }}" 
     required autofocus>

                            @if ($errors->has('role'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('role') }}
 </strong>
                                </span>
                            @endif
            </div>
            <div class="form-group{{ $errors->has('email') ? ' has-error' : 
 '' }}">
                <label for="">E-Mail Address</label>
                <input id="email" type="text" class="form-control" 
 name="email" value="{{ 
   old('email') }}" required autofocus>

                            @if ($errors->has('email'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('email') }}
 </strong>
                                </span>
                            @endif
            </div>
            <div class="form-group">
               <label for="password">Password</label>

                        <div class="form-group">
                            <input id="password" type="password" 
 class="form-control" 
     name="password" value="{{ old('password') }}" required autofocus>

                            @if ($errors->has('password'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('password') }}
  </strong>
                                </span>
                            @endif
                        </div>
            </div>
            <div class="form-group">
               <label for="password">Confirm Password</label>
                        <div class="form-group">
                            <input id="password_confirmation" 
 type="password" class="form-control" 
    name="password" value="{{ old('password_confirmation') }}" required 
 autofocus>
                        </div>
            </div>
            <div class="form-group">
                <label for="zoneintervention">zoneintervention</label>
                <select multiple name="zoneintervention_id[]" 
   id="zoneintervention" class="form-
    control" >

                        @foreach($zoneintervention as $zoneintervention)
                         <option value="{{ $zoneintervention->id }}">
                            {{$zoneintervention->code_postal}}
                         </option>
                        @endforeach
                </select>
            </div>

            <div class="form-group">
                <label for="">Moyenne Avis</label>
                 <input type="text"  name ="moyenne_avis" class="form-
  control"value="
    {{old('moyenne_avis')}}">
            </div>
            <div class="form-group">
                <div class="form-group">
                <label for="">Etat</label>
                <input type="checkbox"  name ="actif" value="1">
            </div>
            </div>
            <div class="form-group">
                <input type="submit" value = "suivant" class="form-control 
 btn btn-primary">
            </div>
        </form>
    </div>
    </div>
@endsection

controler

 public function create()
 {
     $zoneintervention = Zoneintervention::orderBy('id', 'desc')->get();
    $metier = metier::orderBy('libelle_metier', 'desc')->get();
    $tache = tache::orderBy('libelle_tache', 'desc')->get();
    $user = user::orderBy('id', 'desc')->get();

    return view('technicien.create')->with('zoneintervention', 
   $zoneintervention)->with('user', 
    $user);

  }
  /**
  * Store a newly created resource in storage.
  *
     * @param  
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
 {
    $user = new user();
    $user->nom = $request->input('nom');
    $user->prenom = $request->input('prenom');
    $user->tel = $request->input('tel');
    $user->mobil = $request->input('mobil');
    $user->role = $request->input('role');
    $user->email = $request->input('email');
    $user->password = $request->input('password');
    $user->save();

    $technicien = new technicien();
    $technicien->user_id = $user->id;
    $technicien->actif = $request->input('actif');
    $technicien->moyenne_avis = $request->input('moyenne_avis');
    $technicien->save();
    $technicien->zoneintervention()->attach($request->zoneintervention_id);



    return redirect('tarification/create');

}

route.php

Route::get('/technicien', 'TechnicienController@index');
Route::get('/technicien/create', 'TechnicienController@create');
Route::post('/technicien', 'TechnicienController@store');
1
  • You have defined actif fillable in your protected $fillable = ["actif"]; Remove it from there and and add it as a nullable() in migration it fixed! Commented Mar 14, 2018 at 9:19

3 Answers 3

4

Try to add hidden input with zero value, like this:

<input type="hidden" name="actif" value="0">
<input type="checkbox" name="actif" value="1">

So if checkbox is checked, then actif value will be 1, if checkbox is unchecked, then actif value will be 0, because then hidden value will be used.

Sign up to request clarification or add additional context in comments.

2 Comments

when i use this solution i can choose the 2 checkboxes and it does not make sense
2 checkboxes? One input is not checkbox, it's input with type hidden.
2

Use $request->has()

$technicien = new technicien();
$technicien->user_id = $user->id;
if($request->has('actif')){
       $technicien->actif = $request->input('actif');
}else{
       $technicien->actif = 0;
}
$technicien->moyenne_avis = $request->input('moyenne_avis');
$technicien->save();

1 Comment

"Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)"
0

It's an SQLSTATE[23000] Integrity constraint violation error which occurs if the rules you declared in migration file doesn't matches with the input from your form. So in order to resolve this error you need to just add nullable() to your database migration file where you have 'actif' column declared in the up method.

$table->tinyInteger('actif')->nullable();

1 Comment

show your database migration file.. I will edit my answer likewise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.