save information in 2 tables whith one to one relation ship - php

I have 2 table the first table user and the second technicien and the relation is one to one relation ship i like inserte the information of the user and the information of technicien in the same time
user model:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
public function technicien()
{
return $this->hasOne('App\technicien');
}
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'email', 'password','nom','prenom','tel','mobil','role',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
technicien model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class technicien extends Model
{
public function zoneintervention()
{
return $this->belongsToMany('App\zoneintervention','technicien_zone','technicien_id','zoneintervention_id');
}
public function metier()
{
return $this->belongsToMany('App\metier','technicien_metier','technicien_id','metier_id');
}
public function user()
{
return $this->belongsTo('App\User');
}
public function tarificationtache()
{
return $this->hasMany(Tarificationtache::class);
}
}
this is the view create.blade.php
#extends('Layouts/app')
#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>
<div class="col-md-12">
<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="telephone" type="text" class="form-control"
name="telephone" value="{{ old('telephone') }}" required autofocus>
#if ($errors->has('telephone'))
<span class="help-block">
<strong>{{ $errors->first('telephone')
}}</strong>
</span>
#endif
</div>
<div class="form-group">
<label for="">Mobile</label>
<input id="Mobile" type="text" class="form-control"
name="Mobile" value="{{ old('Mobile') }}" required autofocus>
#if ($errors->has('Mobile'))
<span class="help-block">
<strong>{{ $errors->first('Mobile') }}
</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_confirmation"
value="{{
old('password_confirmation') }}" required s>
</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">
<label for="">Etat</label>
<input type="text" name ="actif" class="form-
control"value="{{old('actif')}}">
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-
control btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
some one help me

Related

Laravel7 How to custom the RegisterController to save multiple data when user selected

I am a newbie in Laravel and I need your help.
I'm using the Laravel 7. I have 3 tables in my database.
User to save the registered users
Categories to show in the front-end that I provided for them to choose (multiple)
Vendor_Cat that I collecting the user_id and category_id after the user registered
Here are my Models
public function users(){
return $this->BelongsToMany(User::class);
}
public function categories(){
return $this->BelongsToMany(Categories::class);
}
This one below is my VendorCategories Model
protected $fillable = [
'category_id',
'vendor_id'
];
And this is the Laravel RegisterController
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'tel' => $data['tel'],
'lineid'=> isset($data['lineid']) ? $data['lineid'] : null,
]);
My Front-End code (Just Categories for a user to choose)
#if($categories->count()>0)
<div class="form-row">
<label class="form-row-inner">
<select class="selectpicker col-sm-12 md-12" id="selectpicker" name="categories[]" multiple data-live-search="true" data-selected-text-format="count" data-count-selected-text="Selected Categories({0})" title="Choose your catories" aria-expanded="false" aria-haspopup="true" required>
#foreach ($categories as $categories)
<option class="com-sm-4 md-4" aria-expanded="false" value="{{$categories->id}}"
#if (isset($user))
#if($user->hasCat($catgories->id))
selected
#endif
#endif
><strong>{{Str::limit($categories->name,51)}}</strong></option>
#endforeach
</select>
#error('category_id')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
#endif
Full Register Form Code
<form class="form-detail col-xs-12 sm-12" action="{{ route('register') }}" method="post">
#csrf
<div class="tabcontent" id="sign-up">
<div class="form-row">
<label class="form-row-inner">
<input type="text" id="full_name" class="input-text #error('name') is-invalid #enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
<span class="label">Full Name</span>
#error('name')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<span class="border"></span>
</label>
</div>
<div class="form-row">
<label class="form-row-inner">
<input type="text" id="your_email" class="input-text #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
<span class="label">E-Mail</span>
#error('email')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<span class="border"></span>
</label>
</div>
<div class="form-row">
<label class="form-row-inner">
<input type="text" id="yournumber" class="input-text #error('tel') is-invalid #enderror" name="tel" required autocomplete="new-password">
<span class="label">Telephone Number</span>
#error('tel')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<span class="border"></span>
</label>
</div>
<div class="form-row">
<label class="form-row-inner">
<input type="password" name="password" id="password" class="input-text #error('password') is-invalid #enderror" name="password" required autocomplete="new-password">
<span class="label">Password</span>
#error('password')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<span class="border"></span>
</label>
</div>
<div class="form-row">
<label class="form-row-inner">
<input type="password" id="comfirm_password" class="input-text" name="password_confirmation" required autocomplete="new-password">
<span class="label">Confirm Password</span>
<span class="border"></span>
</label>
</div>
<div class="form-row">
<label class="form-row-inner">
<input type="text" id="comfirm_password_1" class="input-text #error('lineid') is-invalid #enderror" name="lineid" required autocomplete="new-password">
<span class="label">Line Id</span>
#error('lineid')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<span class="border"></span>
</label>
</div>
#if($categories->count()>0)
<div class="form-row">
<label class="form-row-inner">
<select class="selectpicker col-sm-12 md-12" id="selectpicker" name="categories[]" multiple data-live-search="true" data-selected-text-format="count" data-count-selected-text="หมวดหมู่ที่เลือก ({0} หมวดหมู่)" title="เลือกหมวดหมู่ที่ให้บริการ" aria-expanded="false" aria-haspopup="true" required>
#foreach ($categories as $categories)
<option class="com-sm-4 md-4" aria-expanded="false" value="{{$categories->id}}"
#if (isset($user))
#if($user->hasCat($catgories->id))
selected
#endif
#endif
><strong>{{Str::limit($categories->name,51)}}</strong></option>
#endforeach
</select>
#error('category_id')
<span class="invalid-feedback-detail" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
#endif
<div class="form-row-last">
<input type="submit" name="register" class="register">
</div>
</div>
</form>
If I want to save the category Ids that I use #foreach to show in the Register Form after the users selected. What should I do?
This is the thing that I have tried before
protected function create(array $data){
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'tel' => $data['tel'],
'lineid'=> isset($data['lineid']) ? $data['lineid'] : null,
]);
$vendor_cat = VendorCat::create([
'category_id' => $data['category'],
'vendor_id' => $user->id
]);
return $user;
}
Here is the explanation image
Click Here
Thank you,
p.s. sorry for my bad English.
Add registered method in your RegisterController, to override the register method from your RegistersUsers trait. Also add use Illuminate\Http\Request; in your RegisterController.
protected function create(array $data)
{
// .... your create method in RegisterController
}
protected function registered( Request $request, $user )
{
VendorCat::create([
'category_id' => $request['category'],
'vendor_id' => $user->id
]);
}
EDIT:
as you are getting categories as array, you can simply store all the keys separated by commas, or store them separately for each values,
To store them separately,
foreach($request['categories'] as $category){
VendorCat::create([
'category_id' => $category,
'vendor_id' => $user->id
]);
}
It will create 3 columns for 3 different category_id with same vendor_id.
To store using commas,
VendorCat::create([
'category_id' => is_array($request['categories']) ? implode(",",$request['categories']) : $request['categories'],
'vendor_id' => $user->id
]);

how can i get the values of some inputs from database after select option in dropdown

I'm building an HTML form that contains all information about clients, and the others information, so I would when I choose the name of the client that is in the dropdown (select options that contain all name of clients ) the other input fields like (address, tel, email) should be filled automatically.
I don't know what I have to use ajax or jquery and how to use it.
this is my view:
#extends('layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-11">
<div class="panel panel-default">
<div class="panel-heading">Création de commandes</div>
<div class="panel-body">
<form id="form2" class="form-horizontal" method="POST" action="{{url('gestion_commandes/create')}}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('nomp') ? ' has-error' : '' }}">
<label for="nomp" class="col-md-1 control-label">Client</label>
<div class="col-md-5">
<select name="nomclient" id="nomclient" class="form-control dynamic" data-dependent="organisme" required autofocus>
<option>--Select--</option>
#foreach ($clientscombos as $clientcombos)
<option value="{{$clientcombos->nom}}">{{$clientcombos->nom}} </option>
#endforeach
</select>
</div>
{{ csrf_field() }}
<label for="organisme" class="col-md-1 control-label">Organisme</label>
<div class="col-md-5">
<input id="organisme" type="text" class="form-control" name="organisme" value="{{$clientcombos->organisme}}" required autofocus>
#if ($errors->has('organisme'))
<span class="help-block">
<strong>{{ $errors->first('organisme') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('adresse') ? ' has-error' : '' }}">
<label for="adresse" class="col-md-1 control-label">Adresse</label>
<div class="col-md-5">
<input id="adresse" type="text" class="form-control" name="adresse" value="{{$clientcombos->adresse}}" autofocus>
#if ($errors->has('adresse'))
<span class="help-block">
<strong>{{ $errors->first('adresse') }}</strong>
</span>
#endif
</div>
<label for="emailp" class="col-md-1 control-label">E-Mail Address</label>
<div class="col-md-5">
<input id="emailp" type="email" class="form-control" name="emailp" placeholder="Saisir l'adresse email" required autofocus>
#if ($errors->has('emailp'))
<span class="help-block">
<strong>{{ $errors->first('emailp') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('adressep') ? ' has-error' : '' }}">
<label for="tel" class="col-md-1 control-label">Tel</label>
<div class="col-md-3">
<input id="tel" type="Tel" class="form-control" name="tel" value="{{$clientcombos->tel1}}" required autofocus>
#if ($errors->has('tel'))
<span class="help-block">
<strong>{{ $errors->first('tel') }}</strong>
</span>
#endif
</div>
<label for="tel2p" class="col-md-1 control-label">Tel2</label>
<div class="col-md-3">
<input id="tel2p" type="Tel" class="form-control" name="tel2p" value="+2126" required autofocus>
#if ($errors->has('tel2p'))
<span class="help-block">
<strong>{{ $errors->first('tel2p') }}</strong>
</span>
#endif
</div>
<label for="faxp" class="col-md-1 control-label">Fax</label>
<div class="col-md-3">
<input id="faxp" type="Tel" class="form-control" name="faxp" value="{{$clientcombos->fax}}" required autofocus>
#if ($errors->has('faxp'))
<span class="help-block">
<strong>{{ $errors->first('faxp') }}</strong>
</span>
#endif
</div>
</div>
<div class=" panel panel-success"> </div>
<div class=" panel panel-success"> </div>
<div class="form-group{{ $errors->has('daterecep') ? ' has-error' : '' }}">
<label for="commercial" class="col-md-1 control-label">Commercial</label>
<div class="col-md-3">
<select name="commercial" id="commercial" class="form-control" required autofocus>
<option>--Select--</option>
#foreach ($combocommerciaux as $combocommercial)
<option value="{{$combocommercial->name}}">{{$combocommercial->name}} </option>
#endforeach
</select>
</div>
<label for="datereception" class="col-md-1 control-label">Date de réception</label>
<div class="col-md-3">
<input id="datereception" type="date" class="form-control" name="datereception" placeholder="Saisir le nom complet" required autofocus>
#if ($errors->has('datereception'))
<span class="help-block">
<strong>{{ $errors->first('datereception') }}</strong>
</span>
#endif
</div>
<label for="dateprelev" class="col-md-1 control-label">Date de prélevement</label>
<div class="col-md-3">
<input id="dateprelev" type="date" class="form-control" name="dateprelev" placeholder="Saisir le nom complet" required autofocus>
#if ($errors->has('dateprelev'))
<span class="help-block">
<strong>{{ $errors->first('dateprelev') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('commercial') ? ' has-error' : '' }}">
<label for="savedby" class="col-md-9 control-label">Enregistrée par </label>
<div class="col-md-3">
<input id="savedby" type="text" class="form-control" name="savedby" value="{{Auth::user()->name}}" required autofocus>
#if ($errors->has('savedby'))
<span class="help-block">
<strong>{{ $errors->first('savedby') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('nbrrowsol') ? ' has-error' : '' }}">
<label for="nbrrowsow" class="col-md-1 control-label">Sol </label>
<div class="col-md-3">
<input id="nbrrowsol" type="number" class="form-control" name="nbrrowsol" required autofocus>
#if ($errors->has('nbrrowsol'))
<span class="help-block">
<strong>{{ $errors->first('nbrrowsol') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('nbrroweau') ? ' has-error' : '' }}">
<label for="nbrroweau" class="col-md-1 control-label">Eau </label>
<div class="col-md-3">
<input id="nbrroweau" type="number" class="form-control" name="nbrroweau" required autofocus>
#if ($errors->has('nbrroweau'))
<span class="help-block">
<strong>{{ $errors->first('nbrroweau') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('nbrrowveg') ? ' has-error' : '' }}">
<label for="nbrrowveg" class="col-md-1 control-label">VEG </label>
<div class="col-md-3">
<input id="nbrrowveg" type="number" class="form-control" name="nbrrowveg" required autofocus>
#if ($errors->has('nbrrowveg'))
<span class="help-block">
<strong>{{ $errors->first('nbrrowveg') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('nbrrowbiof') ? ' has-error' : '' }}">
<label for="nbrrowbiof" class="col-md-1 control-label">Fertilisant </label>
<div class="col-md-3">
<input id="nbrrowbiof" type="number" class="form-control" name="nbrrowbiof" placeholder="Saisir l'adresse email" required autofocus>
#if ($errors->has('nbrrowbiof'))
<span class="help-block">
<strong>{{ $errors->first('nbrrowbiof') }}</strong>
</span>
#endif
</div>
<div class="col-md-6 control-label">
<button type="submit" class="btn btn-primary" onclick="location.href='{{url('gestion_regions/create')}}'" >Executer</button>
</div>
</div>
<div class="form-group{{ $errors->has('nbrrowmicair') ? ' has-error' : '' }}">
<label for="nbrrowmicair" class="col-md-1 control-label">Mic Air </label>
<div class="col-md-3">
<input id="nbrrowmicair" type="number" class="form-control" name="nbrrowmicair" required autofocus>
#if ($errors->has('nbrrowmicair'))
<span class="help-block">
<strong>{{ $errors->first('nbrrowmicair') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('nbrrowmiceau') ? ' has-error' : '' }}">
<label for="nbrrowmiceau" class="col-md-1 control-label">Mic Eau</label>
<div class="col-md-3">
<input id="nbrrowmiceau" type="number" class="form-control" name="nbrrowmiceau" required autofocus>
#if ($errors->has('nbrrowmiceau'))
<span class="help-block">
<strong>{{ $errors->first('nbrrowmiceau') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('nbrrowmicsurface') ? ' has-error' : '' }}">
<label for="nbrrowmicsurface" class="col-md-1 control-label">Mic Surface </label>
<div class="col-md-3">
<input id="nbrrowmicsurface" type="number" class="form-control" name="nbrrowmicsurface" placeholder="Saisir l'adresse email" required autofocus>
#if ($errors->has('nbrrowmicsurface'))
<span class="help-block">
<strong>{{ $errors->first('nbrrowmicsurface') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-success">
Enregistrer
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
this is controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Client;
class DynamicDependent extends Controller
{
public function comboboxclients()
{
$clientscombos = DB::select('select * from clients');
$combocommerciaux = DB::select('select * from commercials');
return view('gestion_commandes.create',['clientscombos' => $clientscombos , 'combocommerciaux' => $combocommerciaux]);
}
}
You have to:
1) Server-side: create another action which will fetch client data based on value from select(I suggest you using an client id as select value inside your template).
2) Client-side: add hook on select value change('change' event inside JS), which will send an AJAX request(containing selected value) to action created on step 1, and populate form inputs with received data.
That's pretty much it. Fell free to ask about specific implementation details, but keep in mind that no one will write code for you here, you have to do it yourself, but you'll be fine with this directions.

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

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');
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.
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();
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();

make:auth() Login/Register not working

I am working with Laravel 5.5. I created login/register page with make:auth about 1 month ago and didn't change anything inside Laravel auth core files.
I just register and, after that I want to login. Login page reloaded when I hit login button and don't give any error messages. I don't know what's wrong. This code worked during 1 month and from yesterday it doesn't work right.
I just attached my login/register files here.
Auth Register Controller
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller {
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'member_id' => 'required|string|integer|min:000000|max:999999',
'password' => 'required|string|min:6|confirmed',
]);
}
protected function create(array $data)
{
return User::create([
'member_id' => $data['member_id'],
'password' => bcrypt($data['password']),
]);
}
This is my register.blade.php
<form class="form-horizontal" method="POST" action="{{ route('register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('member_id') ? ' has-error' : '' }}">
<label for="member_id" class="col-md-4 control-label">
{{ Lang::get('all.info.member_id') }}
</label>
<div class="col-md-6">
<input id="member_id" type="number" onkeypress='return event.charCode >= 48 && event.charCode <= 57' class="form-control" name="member_id" value="{{ old('member_id') }}" required>
#if ($errors->has('member_id'))
<span class="help-block">
<strong>{{ $errors->first('member_id') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">
{{ Lang::get('all.info.password') }}
</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#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" class="col-md-4 control-label">
{{ Lang::get('all.info.confirm_password') }}
</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
</form>
This is my login.blade.php
<form class="form-horizontal" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('scout_id') ? ' has-error' : '' }}">
<label for="scout_id" class="col-md-4 control-label">
{{ Lang::get('all.login_p.scout_id') }}
</label>
<div class="col-md-6">
<input id="member_id" type="number" onkeypress='return event.charCode >= 48 && event.charCode <= 57' class="form-control" name="member_id" value="{{ old('member_id') }}" required autofocus>
#if ($errors->has('member_id'))
<span class="help-block">
<strong>{{ $errors->first('member_id') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">
{{ Lang::get('all.login_p.password') }}
</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
{{ Lang::get('all.login') }}
</button>
</div>
</div>
</form>
did you use Auth::attempt(); ?
Auth::attempt(['member_id' => $request->member_id, 'password' => $request->password]);
You are not telling the LoginController that your 'email'/'username' field is not email. You have to tell it you want to use a different field.
Username Customization
"By default, Laravel uses the email field for authentication. If you would like to customize this, you may define a username method on your LoginController:"
public function username()
{
return 'member_id';
}
Laravel 5.5 Docs - Authentication - Authentication Quickstart - Authenticating

Storing data in neo4j graph DB

I've written a simple HTML code for a form:
Form code
<form method="POST" action="/ducks" novalidate>
<div class="form-group #if ($errors->has('name')) has-error #endif">
<label for="name">Name</label>
<input type="text" id="name" class="form-control" name="name" placeholder="Somebody Awesome" value="{{ Input::old('name') }}">
#if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('email')) has-error #endif">
<label for="email">Email</label>
<input type="text" id="email" class="form-control" name="email" placeholder="super#cool.com" value="{{ Input::old('email') }}">
#if ($errors->has('email')) <p class="help-block">{{ $errors->first('email') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('password')) has-error #endif">
<label for="password">Password</label>
<input type="password" id="password" class="form-control" name="password">
#if ($errors->has('password')) <p class="help-block">{{ $errors->first('password') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('password_confirm')) has-error #endif">
<label for="password_confirm">Confirm Password</label>
<input type="password" id="password_confirm" class="form-control" name="password_confirm">
#if ($errors->has('password_confirm')) <p class="help-block">{{ $errors->first('password_confirm') }}</p> #endif
</div>
<button type="submit" class="btn btn-success">Submit!</button>
</form>
php code
<?php
$formdata = Neo4j::makeNode();
$formdata->setProperty('frname', 'fname')
->setProperty('lsname', 'lname')
->setProperty('pname', 'pword')
->setProperty('mail','email')
->save();
$formdataId = $formdata->getId();
?>
And I've added a migration code as shown above. Controllers are good, I think so.
But the data isn't getting stored in neo4j DB. How can I fix it?

Categories