如果未选中则如何传递复选框值0,如果使用数组选中则传递1

时间:2018-03-14 09:13:59

标签: php laravel

我选中了一个复选框,我在数据库中有值' 1'但是,当我不选择我有这个错误 {SQLSTATE [23000]:违反完整性约束:1048列' actif'不能为空(SQL:插入techniciensuser_idactifmoyenne_avisupdated_atcreated_at)值(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');

3 个答案:

答案 0 :(得分:1)

使用$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();

do_stuff_parallel

答案 1 :(得分:1)

尝试添加零值的隐藏输入,如下所示:

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

因此,如果选中复选框,则actif值将为1,如果未选中复选框,则actif值将为0,因为将使用隐藏值。

答案 2 :(得分:0)

如果您在迁移文件中声明的规则与表单中的输入不匹配,则会发生SQLSTATE [23000]完整性约束违规错误。因此,为了解决此错误,您只需将 nullable()添加到数据库迁移文件中,其中您在up方法中声明了'actif'列。

login(userName, password): Observable<any> {

    const loginData = 'username=' + userName + '&password=' + password;

    const authUrlParam = 'rest/auth/j_spring_security_check';
    return this.ajaxService.postForLogin(authUrlParam, loginData, 'application/x-www-form-urlencoded');
  }

postForLogin(url: string, data: any, contentType: string): Observable<any> {
    console.log('Posting Data to Server url : ' + environment.serverUrl + url);

    const headers = new Headers();
    headers.append('Content-Type', contentType);
    const reqOp = new RequestOptions({ headers: headers, withCredentials: true });

    const returnResponse = this.http
      .post(environment.serverUrl + url, data, reqOp)
      .map((response: Response) => response.json().data)
      .catch(this.handleError);
    return returnResponse;
  }