Quiz questions create - php

Hello i'm building quiz system and i'm stuck on this problem.
Trying to save quiz but it's errors
<form action="/admin/quiz/store" method="POST">
#csrf
<div class="form-group">
<label for="title">ქვიზის დასახელება</label>
<input name="title[title]" type="text" class="form-control" value="{{ old('title.title') }}" id="title" placeholder="">
#error('title.title')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question2">ქვიზის შეკითხვა</label>
<input name="questions[1][question]" type="text" class="form-control" value="{{ old('questions.0.question') }}" id="question1" placeholder="">
#error('questions.0.question')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<fieldset>
<div>
<div class="form-group">
<label for="question1answer1">ქვიზის პასუხი</label>
<input name="questions[1][answers][]" type="text" class="form-control" value="{{ old('answers.0.answer') }}" id="question1answer1" aria-describedby="choicesHelp" placeholder="">
#error('answers.0.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question1answer2">ქვიზის პასუხი</label>
<input name="questions[1][answers][]" type="text" class="form-control" value="{{ old('answers.1.answer') }}" id="question1answer2" aria-describedby="choicesHelp" placeholder="">
#error('answers.1.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question1answer3">ქვიზის პასუხი</label>
<input name="questions[1][answers][]" type="text" class="form-control" value="{{ old('answers.2.answer') }}" id="question1answer3" aria-describedby="choicesHelp" placeholder="">
#error('answers.2.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question1answer4">ქვიზის პასუხი</label>
<input name="questions[1][answers][]" type="text" class="form-control" value="{{ old('answers.3.answer') }}" id="question1answer4" aria-describedby="choicesHelp" placeholder="">
#error('answers.3.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
</div>
</fieldset>
</div>
meore shekitxva
<div class="form-group">
<label for="question2">ქვიზის შეკითხვა</label>
<input name="questions[2][question]" type="text" class="form-control" value="{{ old('questions.0.question') }}" id="question2" placeholder="">
#error('questions.0.question')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<fieldset>
<div>
<div class="form-group">
<label for="question2answer1">ქვიზის პასუხი</label>
<input name="questions[2][answers][]" type="text" class="form-control" value="{{ old('answers.0.answer') }}" id="question2answer1" aria-describedby="choicesHelp" placeholder="">
#error('answers.0.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question2answer2">ქვიზის პასუხი</label>
<input name="questions[2][answers][]" type="text" class="form-control" value="{{ old('answers.1.answer') }}" id="question2answer2" aria-describedby="choicesHelp" placeholder="">
#error('answers.1.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question2answer3">ქვიზის პასუხი</label>
<input name="questions[2][answers][]" type="text" class="form-control" value="{{ old('answers.2.answer') }}" id="question2answer3" aria-describedby="choicesHelp" placeholder="">
#error('answers.2.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question2answer4">ქვიზის პასუხი</label>
<input name="questions[2][answers][]" type="text" class="form-control" value="{{ old('answers.3.answer') }}" id="question2answer4" aria-describedby="choicesHelp" placeholder="">
#error('answers.3.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
</div>
</fieldset>
</div>
<button type="submit" class="btn btn-primary">ქვიზის დამატება</button>
</form>
Its gives Undefined array key "answers" so i really don't understand why it's giving me this error...
I was creating like this answers[][answer] but it was saving quiz title and questions normal but in answers table it was saving first question answers in question_id=1 and question_id=2 and it's saves second question same.
Then someone told me to do questions[1][answers][] but now i'm getting Undefined array key "answers"
saving controller
public function store(Quizze $quizzes)
{
$data = request()->validate([
'title.title' => 'required',
'questions.*.question' => 'required',
'answers.*.answer' => 'required',
], [
'title.title.required' => 'გთხოვთ, შეიყვანოთ ქვიზის სახელი.',
'questions.*.question.required' => 'გთხოვთ, შეიყვანოთ შეკითხვა.',
'answers.*.answer.required' => 'გთხოვთ, შეიყვანოთ პასუხი.'
]);
$storeQuiz = $quizzes->create($data['title']);
foreach ($data['questions'] as $q) {
$question = $storeQuiz->questions()->create($q);
$question->answers()->createMany($data['answers']);
}
return redirect('admin/quizzes');
}
please can someone help me with this...
sorry for my bad english.

You need to update your loop :
foreach ($data['questions'] as $key => $q) {
$question = $storeQuiz->questions()->create(['question' => $q['question']]);
$question->answers()->createMany($data['questions'][$key]['answers']);
}
Update
There's no answers data (ref by comment), because validation. You need to change validation :
'title.title' => 'required',
'questions.*.question' => 'required',
'questions.*.answers.*.answer' => 'required',
And HTML :
<form action="/admin/quiz/store" method="POST">
#csrf
<div class="form-group">
<label for="title">ქვიზის დასახელება</label>
<input name="title[title]" type="text" class="form-control" value="{{ old('title.title') }}" id="title" placeholder="">
#error('title.title')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question2">ქვიზის შეკითხვა</label>
<input name="questions[1][question]" type="text" class="form-control" value="{{ old('questions.0.question') }}" id="question1" placeholder="">
#error('questions.0.question')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<fieldset>
<div>
<div class="form-group">
<label for="question1answer1">ქვიზის პასუხი</label>
<input name="questions[1][answers][][answer]" type="text" class="form-control" value="{{ old('questions.1.answers.0.answer') }}" id="question1answer1" aria-describedby="choicesHelp" placeholder="">
#error('questions.1.answers.0.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question1answer2">ქვიზის პასუხი</label>
<input name="questions[1][answers][][answer]" type="text" class="form-control" value="{{ old('questions.1.answers.1.answer') }}" id="question1answer2" aria-describedby="choicesHelp" placeholder="">
#error('questions.1.answers.1.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question1answer3">ქვიზის პასუხი</label>
<input name="questions[1][answers][][answer]" type="text" class="form-control" value="{{ old('questions.1.answers.2.answer') }}" id="question1answer3" aria-describedby="choicesHelp" placeholder="">
#error('questions.1.answers.2.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question1answer4">ქვიზის პასუხი</label>
<input name="questions[1][answers][][answer]" type="text" class="form-control" value="{{ old('questions.1.answers.3.answer') }}" id="question1answer4" aria-describedby="choicesHelp" placeholder="">
#error('questions.1.answers.3.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
</div>
</fieldset>
</div>
meore shekitxva
<div class="form-group">
<label for="question2">ქვიზის შეკითხვა</label>
<input name="questions[2][question]" type="text" class="form-control" value="{{ old('questions.0.question') }}" id="question2" placeholder="">
#error('questions.0.question')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<fieldset>
<div>
<div class="form-group">
<label for="question2answer1">ქვიზის პასუხი</label>
<input name="questions[2][answers][][answer]" type="text" class="form-control" value="{{ old('questions.2.answers.0.answer') }}" id="question2answer1" aria-describedby="choicesHelp" placeholder="">
#error('questions.2.answers.0.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question2answer2">ქვიზის პასუხი</label>
<input name="questions[2][answers][][answer]" type="text" class="form-control" value="{{ old('questions.2.answers.1.answer') }}" id="question2answer2" aria-describedby="choicesHelp" placeholder="">
#error('questions.2.answers.1.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question2answer3">ქვიზის პასუხი</label>
<input name="questions[2][answers][][answer]" type="text" class="form-control" value="{{ old('questions.2.answers.2.answer') }}" id="question2answer3" aria-describedby="choicesHelp" placeholder="">
#error('questions.2.answers.2.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
<div class="form-group">
<label for="question2answer4">ქვიზის პასუხი</label>
<input name="questions[2][answers][][answer]" type="text" class="form-control" value="{{ old('questions.2.answers.3.answer') }}" id="question2answer4" aria-describedby="choicesHelp" placeholder="">
#error('questions.2.answers.3.answer')
<small class="text-danger">{{ $message }}</small>
#enderror
</div>
</div>
</fieldset>
</div>
<button type="submit" class="btn btn-primary">ქვიზის დამატება</button>
</form>

Related

laravel 7 eye hidden

i try to add hide eye in password in laravel project in register. i find code in codepen but when i add
<span toggle="#password-field" class="fa fa-fw fa-eye field-icon toggle-password"></span>
Register
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-12">
<h1 style="border-bottom: solid yellow;"><strong>Formulaire d'inscription</strong></h1>
<div class="card-body">
<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
#csrf
<h3><strong>Vos identifiants</strong></h3>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">Adresse mail</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">Mot de passe</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="new-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<div class="input-group-addon">
<i class="fa fa-eye-slash" aria-hidden="true"></i>
</div>
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">Confirmation mot de passe</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<h3><strong>Informations personnelles</strong></h3>
<div class="form-group row">
<label for="sex" class="col-md-4 col-form-label text-md-right">Civilité</label>
<div class="col-md-1">
<div class="form-check">
<input class="form-check-input" type="radio" name="sexe" id="exampleRadios1" value="M" checked>
<label class="form-check-label" for="exampleRadios1">
Male
</label>
</div>
</div>
<div class="col-md-1">
<div class="form-check">
<input class="form-check-input" type="radio" name="sexe" id="exampleRadios2" value="F">
<label class="form-check-label" for="exampleRadios2">
Female
</label>
</div>
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">Prénom</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="firstname" class="col-md-4 col-form-label text-md-right">Nom</label>
<div class="col-md-6">
<input id="firstname" type="text" class="form-control #error('firstname') is-invalid #enderror" name="firstname" value="{{ old('firstname') }}" required autocomplete="firstname" autofocus>
#error('firstname')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="address" class="col-md-4 col-form-label text-md-right">Adresse</label>
<div class="col-md-6">
<input id="address" type="text" class="form-control #error('address') is-invalid #enderror" name="address" value="{{ old('address') }}" required autocomplete="address" autofocus>
#error('address')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="city" class="col-md-4 col-form-label text-md-right">Ville</label>
<div class="col-md-6">
<input id="city" type="text" class="form-control #error('city') is-invalid #enderror" name="city" value="{{ old('city') }}" required autocomplete="city" autofocus>
#error('city')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="zipcode" class="col-md-4 col-form-label text-md-right">Code postale</label>
<div class="col-md-6">
<input id="zipcode" type="text" class="form-control #error('zipcode') is-invalid #enderror" name="zipcode" value="{{ old('zipcode') }}" required autocomplete="zipcode" autofocus>
#error('zipcode')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-md-4 col-form-label text-md-right">Numéro de téléphone</label>
<div class="col-md-6">
<input id="phone" type="text" class="form-control #error('phone') is-invalid #enderror" name="phone" value="{{ old('phone') }}" required autocomplete="phone" autofocus>
#error('phone')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="birthday" class="col-md-4 col-form-label text-md-right">Date de naissance</label>
<div class="col-md-6">
<input id="birthday" type="date" class="form-control #error('birthday') is-invalid #enderror" name="birthday" value="{{ old('birthday') }}" required autocomplete="birthday" autofocus>
#error('birthday')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="pseudo" class="col-md-4 col-form-label text-md-right">Nom d'utilisateur (pseudo)</label>
<div class="col-md-6">
<input id="pseudo" type="text" class="form-control #error('pseudo') is-invalid #enderror" name="pseudo" value="{{ old('pseudo') }}" required autocomplete="pseudo" autofocus>
#error('pseudo')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="avatar" class="col-md-4 col-form-label text-md-right"></label>
<div class="col-md-6">
<input id="avatar" type="file" class="form-control #error('avatar') is-invalid #enderror" name="avatar" autocomplete="avatar" autofocus>
#error('avatar')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<div class="col-md-6 offset-4">
<div class="g-recaptcha"
data-sitekey="{{env('GOOGLE_RECAPTCHA_KEY')}}">
</div>
<span role="alert" class="invalid-feedback d-block">
#error('g-recaptcha-response')
<strong>{{ $errors->first('g-recaptcha-response') }}</strong>
#enderror
</span>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Crée mon compte
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#show_hide_password a").on('click', function(event) {
event.preventDefault();
if($('#show_hide_password input').attr("type") == "text"){
$('#show_hide_password input').attr('type', 'password');
$('#show_hide_password i').addClass( "fa-eye-slash" );
$('#show_hide_password i').removeClass( "fa-eye" );
}else if($('#show_hide_password input').attr("type") == "password"){
$('#show_hide_password input').attr('type', 'text');
$('#show_hide_password i').removeClass( "fa-eye-slash" );
$('#show_hide_password i').addClass( "fa-eye" );
}
});
});
</script>
#endsection
#section('scripts')
<script src='https://www.google.com/recaptcha/api.js'></script>
#endsection
not have something, i try to add code from there :
https://codepen.io/Qanser/pen/dVRGJvm but its not work for me i think i do something not good someone can help me add eye? Nothing appears, it's been 2 hours already I'm on it I can't take it anymore XD Someone
you can see my screen shot in this link : ibb.co/W2kQTj8
Have mercy on a noob help me XD
make sure that the page layouts.app that you extend have the link to fa eye class like one bellow
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
</head>
<body>
<i class="fa fa-eye"></i>
</body>
</html>

redirect back to register page then add flash message upon registering using the make:auth() in Laravel

Hi i have this laravel app. Im using the make:auth() for my login and registration system. By default the registration will automatically goes to /home page. Now, i want to redirect my register page to itself then add a flash message to it. Not automatically login. I also added the Session flash message code to it. How will i able to achieve this? this is my code below. Can someone help me figured this thing out?
Any help is muchly appreciated.TIA
app\http\controllers\auth\RegisterController.php :
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'firstName' => 'required|string|max:255',
'middleName'=> 'required|string|max:255',
'lastName'=> 'required|string|max:255',
'address'=> 'required|string|max:255',
'contactNumber'=> 'required|string|max:255',
'username'=> 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return \App\User
*/
protected function create(array $data)
{
$membersNumber = time() . rand(10*45, 100*98);
Session::flash('success', 'You are successfully registered! Serapio.ph will review your submitted documents and will text you if you can login.');
return User::create([
'members_number'=>$membersNumber,
'first_name' => $data['firstName'],
'middle_name'=>$data['middleName'],
'last_name'=>$data['lastName'],
'address'=>$data['address'],
'contact_number'=>$data['contactNumber'],
'username'=>$data['username'],
'reference_person'=>$data['referencePerson'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
register.blade.php :
#extends('layouts.app')
#section('title', 'Register | Serapio.ph')
#section('content')
<!-- BREADCRUMBS -->
<div class="page-header">
<div class="container">
<h1 class="page-title pull-left">Register</h1>
<ol class="breadcrumb">
<li>Home</li>
<li class="active">Register</li>
</ol>
</div>
</div>
<!-- END BREADCRUMBS -->
<div class="container">
<div class="row">
<form class="form-horizontal" method="POST" action="{{ route('register') }}">
<div class="col-md-6 form-horizontal">
<!-- REGISTRATION FORM -->
<br>
<h2 class="section-heading">Member's Sign Up Info</h2>
{{ csrf_field() }}
#if (session('success'))
<p class="alert alert-success">{{ Session::get('success') }}</p>
#endif
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<input type="text" class="form-control" id="firstName" name="firstName" value="{{ old('firstName') }}" placeholder="First Name" />
<span class="input-group-addon"><i class="fa fa-user"></i></span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<input type="text" class="form-control" id="middleName" name="middleName" value="{{ old('middleName') }}" placeholder="Middle Name" />
<span class="input-group-addon"><i class="fa fa-user"></i></span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<input type="text" class="form-control" id="lastName" name="lastName" value="{{ old('lastName') }}" placeholder="Last Name" />
<span class="input-group-addon"><i class="fa fa-user"></i></span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<input type="text" class="form-control" id="address" name="address" value="{{ old('address') }}" placeholder="Address" />
<span class="input-group-addon"><i class="fa fa-map-pin"></i></span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<input type="text" class="form-control" id="contactNumber" name="contactNumber" value="{{ old('contactNumber') }}" placeholder="Contact Number" />
<span class="input-group-addon"><i class="fa fa-phone-square"></i></span>
</div>
</div>
</div>
</div>
<div class="col-md-6 form-horizontal" style='margin-top:95px;'>
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<input type="text" class="form-control" id="referencePerson" name="referencePerson" value="" placeholder="Reference Person (Optional)" />
<span class="input-group-addon"><i class="fa fa-user"></i></span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="input-group">
<input type="text" class="form-control" id="username" name="username" value="{{ old('username') }}" placeholder="Username" />
<span class="input-group-addon"><i class="fa fa-user"></i></span>
</div>
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="control-label sr-only">Email</label>
<div class="col-sm-12">
<div class="input-group">
<input type="email" class="form-control" id="email" name="email" value="{{ old('email') }}" placeholder="Email">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="control-label sr-only">Password</label>
<div class="col-sm-12">
<div class="input-group">
<input type="password" class="form-control" id="password" name="password" value="" placeholder="Password">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
</div>
<div class="form-group">
<label for="password2" class="control-label sr-only">Repeat Password</label>
<div class="col-sm-12">
<div class="input-group">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required placeholder="Repeat Password">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<label style="color:red;">Note* Upload Seaman's Book/Students ID<br><i>Please upload file using PDF or JPG</i></label>
<input type="file" name="pdf" />
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="checkbox" name="checkbox" /> Terms and Condition
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<button type="submit" class="pull-right btn btn-success btn-lg"><i class="fa fa-check-circle"></i> Create Account</button>
</div>
</div>
</div>
</form>
<!--<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Register</div>
<div class="panel-body">
<form class="form-horizontal" method="POST" action="{{ route('register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">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">Confirm Password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
</div>
</div>
</form>
</div>
</div>
</div>-->
</div>
</div>
#endsection

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

save() method only writing last record in my form

I'm trying to save multiple form records in my database, but it is only writing last record in my blade loop.
//This is my loop in blade. for getting multiple informations
#for ($i = 1; $i < $package->package_for + 1; $i++)
#for ($i = 1; $i < $package->package_for + 1; $i++)
<div class="container information">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><h3>Guest {{ $i }}</h3></div>
<div class="panel-body">
<form class="form-horizontal" action="{{ route('information', ['id' => $package->id]) }}" role="form" method="POST">
<div class="col-md-9 col-md-offset-2 fullname">
<div class="gender">
<label> &nbsp</label>
<select type="text" id="gender" class="form-control col-md-1" name="gender" required>
<option value="1">Mr</option>
<option value="2">Mrs</option>
</select>
</div>
<div class="form-group{{ $errors->has('firstname') ? ' has-error' : '' }}">
<label> First Name</label>
<input id="firstname" placeholder="First Name" type="text" class="form-control" name="firstname" required>
#if ($errors->has('firstname'))
<span class="help-block">
<strong>{{ $errors->first('firstname') }}</strong>
</span>
#endif
</div>
<div class="form-group{{ $errors->has('lastname') ? ' has-error' : '' }}">
<label> First Name</label>
<input id="lastname" placeholder="Last Name" type="text" class="form-control" name="lastname" required>
#if ($errors->has('lastname'))
<span class="help-block">
<strong>{{ $errors->first('lastname') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('contact_number') ? ' has-error' : '' }}">
<label for="contact_number" class="col-md-4 control-label">&nbsp</label>
<div class="col-md-6">
<label>Contact Number</label>
<input id="contact_number_number" placeholder="Contact Number" type="contact_number" class="form-control" name="contact_number" required>
#if ($errors->has('contact_number'))
<span class="help-block">
<strong>{{ $errors->first('contact_number') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('age') ? ' has-error' : '' }}">
<label for="age" class="col-md-4 control-label">&nbsp</label>
<div class="col-md-6">
<label>Age</label>
<select type="text" id="age" class="form-control col-md-1" name="age" required>
#for ($b = 1; $b < 50; $b++)
<option value="$i">{!! $b !!}</option>
#endfor
</select>
#if ($errors->has('age'))
<span class="help-block">
<strong>{{ $errors->first('age') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('address') ? ' has-error' : '' }}">
<label for="address" class="col-md-4 control-label">&nbsp</label>
<div class="col-md-6">
<label>Address</label>
<input id="address" placeholder="Address" type="address" class="form-control" name="address" value="{{ old('address') }}" required>
#if ($errors->has('address'))
<span class="help-block">
<strong>{{ $errors->first('address') }}</strong>
</span>
#endif
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endfor
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary" href="{{ route('package.addToCart', ['id' => $package->id]) }}">
Book Package
</button>
{{ csrf_field() }}
</div>
</div>
</form>

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