How to pass Checkbox value 0 if not checked and 1 if checked using array.

Posted 8 months ago by chagouani

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', '[email protected]');
Route::get('/technicien/create', '[email protected]');
Route::post('/technicien', '[email protected]');

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.