how to store answer into a database - laravel

I have created a multiple choice question and able to store them into a database, I need when a user select the correct answer it stores the id of the question and the answer. Please anyone who can help me how to do it
Here is my form for creating the questions
#foreach($qns as $qn)
{{$qn->question_name}}
<div class="form-check">
<label class="form-check-label" for="opt1">
<input type="radio" class="form-check-input" id="opt1"
name="opt{{ $qn->id }}" value="a">{{$qn->opt1}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt2">
<input type="radio" class="form-check-input" id="opt2"
name="opt{{ $qn->id }}" value="b">{{$qn->opt2}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt3">
<input type="radio" class="form-check-input" id="opt3"
name="opt{{ $qn->id }}" value="c">{{$qn->opt3}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt4">
<input type="radio" class="form-check-input" id="opt4"
name="opt{{ $qn->id }}" value="d">{{$qn->opt4}}
</label>
</div>
#endforeach
Here is my controller
public function store(Request $request){
// dd($request->all());
$this->validate($request,[
'question_id' =>'required|string',
'opt'.$qn->id =>'required|string',
]);
$qn= Answer::create([
'question_id'=>$request['question_id'],
'opt'=>$request['opt'.$qn->id],
]);
}

Do it like
public function saveAnswers(Request $request)
{
$questions = Questions::all();
foreach($questions as $qns){
$name = "opt".$qns->id;
Answer::create([
'question_id'=>$qns->id,
'answer'=>$request->$name,
]);
}
}

Related

Laravel : Next Record in a random Order

im creating a quiz app for driving lessons. the quiz page have all the data but i want to do next button to take you to the next question but in in a random order.
this is my controller's function for the next but it's not working But it worked with me in another project
public function submitans(Request $request) {
$nextq = Session::get('nextq');
$wrongans = Session::get('wrongans');
$correctans = Session::get('correctans');
$validate = $request->validate([
'ans' => "required",
'dbans' => 'required'
]);
$nextq = Session::get('nextq');
$nextq += 1;
if ($request->dbans == $request->ans) {
$correctans += 1;
} else {
$wrongans += 1;
}
Session::put("nextq", $nextq);
Session::put("wrongans", $wrongans);
Session::put("correctans", $correctans);
$i = 0;
$questions = question::all();
foreach ($questions as $question) {
$i++;
if ($questions->count() < $nextq) {
return view('pages.end');
}
if ($i == $nextq) {
// $question = Question::where('id', '>', $question->id)->orderBy('id')->first();
return view('pages.questions.quiz')->with(['question' => $question]);
}
}}
and this and this is a part of the view :
<form method="POST" action="/submitans">
#csrf
<h5 class="mt-1 ml-2">{{ $question->title }}</h5>
</div>
<div class="text-center">
<img src="{{ asset('storage/' . $question->image) }}" alt="image" class="rounded">
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="flexCheckDefault"
name="ans" checked="true" />
<label class="form-check-label" for="flexCheckDefault">{{ $question->reponse1 }}</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="flexCheckDefault"
name="ans" />
<label class="form-check-label" for="flexCheckDefault">{{ $question->reponse2 }}</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="flexCheckDefault"
name="ans" />
<label class="form-check-label" for="flexCheckDefault">{{ $question->reponse3 }}</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="flexCheckDefault"
name="ans" />
<label class="form-check-label" for="flexCheckDefault">{{ $question->reponse4 }}</label>
</div>
<input value="{{ $question->ans }}" style="visibility: hidden" name="dbans">
</div>
<div class="d-flex flex-row justify-content-between align-items-center p-3 bg-white">
<button class="btn btn-primary border-success align-items-center btn-success"
type="submit">Next<i class="fa fa-angle-right ml-2"></i></button>
and the routing is :
Route::get('/quiz',[QuestionController::class,'index']);
Route::any('/submitans', [QuestionController::class, 'submitans']);

duplicate data in blade when send data

i´m trying to show all my roles in my edit blade and with checkbox, that this checks it´s checked if this role have this permission.
my problem it´s when send data permissions and permissions to this role, all permission name it´s duplicate.
for set roles and permission i´m using laravel-permission/spatie
in my controller i have this:
public function edit($id)
{
$role = Role::find($id);
$permissions = Permission::all();
$permissionAssigned = Role::find($id)->givePermissionTo();
return view('opciones.roles.edit', ['role' => $role, 'permissions' => $permissions,
'permissionAssigned' => $permissionAssigned["permissions"] ]);
}
and in my blade i have this:
<h3>Permissions</h3>
<div class="row flex-row justify-content-center">
#foreach($permissions as $permission)
#foreach ($permissionAssigned as $asigned)
#if($permission->name == $asigned->name)
<div class="form-check p-4">
<input class="form-check-input" checked type="checkbox" name="permission[]" value="{{ $permission->name }}" id="flexCheckDefault">
<label class="form-check-label" for="flexCheckDefault">{{ $permission->name }}</label>
</div>
#endif
#endforeach
<div class="form-check p-4">
<input class="form-check-input" type="checkbox" name="permission[]" value="{{ $permission->name }}" id="flexCheckDefault">
<label class="form-check-label" for="flexCheckDefault">{{ $permission->name }}</label>
</div>
#endforeach
</div>
i don´t know that i´m doing wrong. I know that i have a logic problem, but i can´t solve it.
Thanks for readme and sorry for my bad english
You just need to pass the Role with associated Permissions and all Perrmissions to compare against, from the controller.
public function edit($id)
{
$role = Role::with('permissions')->find($id);
$permissions = Permission::all();
return view('opciones.roles.edit', ['role' => $role]);
}
In the view
<h3>Permissions</h3>
<div class="row flex-row justify-content-center">
#foreach($permissions as $permission)
<div class="form-check p-4">
<input type="checkbox"
id="flexCheckDefault"
class="form-check-input"
name="permission[]"
value="{{ $permission->name }}"
#checked((old($permission->name, $role->permissions->contains('name', $permission->name)) />
<label class="form-check-label" for="flexCheckDefault">{{ $permission->name }}</label>
</div>
#endforeach
</div>

Compact Two Functions in One View In Laravel

In my Controller
public function startQ(Request $request){
$users = question::limit(2)->get();
return view('answerDesk',compact('users'));
}
public function answerQuestions(Request $request){
$answers =$request->all();
return view('answerDesk',compact('answers'));
}
in My View
<form action="getAnswer" method="post" role="search"> #csrf #foreach($users as $answer) <input type="hidden" name="question[]" value="{{$answer->id}}">
<label class="form-check-label" for="inlineRadio1">
<h4 style=""><i class="fas fa-circle"></i>
{{$answer->id}}:{{$answer->question}}
</h4>
</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->a}}">(A) : <label class="form-check-label" for="inlineRadio1">{{$answer->a}}</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->b}}">(B) : <label class="form-check-label" for="inlineRadio1">{{$answer->b}}</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->c}}">(C) : <label class="form-check-label" for="inlineRadio1">{{$answer->c}}</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->d}}">(D) : <label class="form-check-label" for="inlineRadio1"> {{$answer->d}}</label><br>
<input type="hidden" name="ans[{{$answer->question}}]" value="{{$answer->ans}}"> #endforeach <input type="submit" value="submit">
</form>
I don't know how to use the getAnswer function
Can I Compact Two Functions in Controller To One View In Laravel?
If Yes How can I Do That?

Retrieving question from database and display it in radio button

I have a questions table, I managed to create a form and send the data to database, i need a user to answer the question by selecting the best answer from radio button list, i have retrieved the question from a database but the radio button does not work correct, when i select the answer from the first question and the when a go to second question the first question radio button is unchecked, i have attached my code. Please anyone who can help me
my view code
<h2>Welcome to Online Teacher Verification Test:</h2>
#foreach($qns as $qn)
{{$qn->question_name}}
<div class="form-check">
<label class="form-check-label" for="opt1">
<input type="radio" class="form-check-input" id="opt1"
name="opt" value="opt1">{{$qn->opt1}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt2">
<input type="radio" class="form-check-input" id="opt2"
name="opt" value="opt2">{{$qn->opt2}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt3">
<input type="radio" class="form-check-input" id="opt3"
name="opt" value="opt3">{{$qn->opt3}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt4">
<input type="radio" class="form-check-input" id="opt4"
name="opt" value="opt4">{{$qn->opt4}}
</label>
</div>
#endforeach
my controller code
use App\Topics;
use App\Questions;
class QuestionController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$qns= Questions::all();
return view('test.index')->with('qns',$qns);
}
I need when a user answer a question by selecting an answer from button, when he/she moves to the second question the first question should remain checked
Well all the radio buttons have the same name when dom renders. So if you check the first one and then the second, the first one becomes unchecked. Try something like
#foreach($qns as $qn)
{{$qn->question_name}}
<div class="form-check">
<label class="form-check-label" for="opt1">
<input type="radio" class="form-check-input" id="opt1"
name="opt{{ $qn->id }}" value="opt1">{{$qn->opt1}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt2">
<input type="radio" class="form-check-input" id="opt2"
name="opt{{ $qn->id }}" value="opt2">{{$qn->opt2}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt3">
<input type="radio" class="form-check-input" id="opt3"
name="opt{{ $qn->id }}" value="opt3">{{$qn->opt3}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt4">
<input type="radio" class="form-check-input" id="opt4"
name="opt{{ $qn->id }}" value="opt4">{{$qn->opt4}}
</label>
</div>
#endforeach
This way you will get different name for each questions and you can check for each question. Well now if you want save them to database you can access the answers in controller with different names try like
public function saveAnswers(Request $request)
{
$questions = Questions::all();
foreach($questions as $qns){
$name = "opt".$qns->id;
$answer = $request->$name;
//here you can save or anything you want to do
}
}
Set name of radio button like name="qn{{ $qn->id }}" so that it is unique.

How to retrieve the post value and validate in controller using laravel?

In my route file
Route::get('griev_reg_form', 'GrievanceRegisterController#show');
Route::post('griev_reg_form', 'GrievanceRegisterController#postdata');
and in controller
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\GrievanceRegister;
use App\Department;
use Input;
use Validator;
use Redirect;
use Session;
class GrievanceRegisterController extends Controller
{
public function show()
{
$departments = Department::orderBy('edesc','ASC')->get();
return view('griev_reg_form', array('departments' => $departments));
}
public function postdata()
{
$data = Input::all();
// print_r($data);
echo $name = Input::get('name');
$rules = array(
'name' => 'required',
'email_address' => 'required|email',
'g-recaptcha-response' => 'required|captcha',
);
$validator = Validator::make($data, $rules);
if ($validator->fails()){
echo "1";
return Redirect::to('/griev_reg_form')->withInput()->withErrors($validator);
}
else{
// Do your stuff.
}
}
}
?>
and in the view file
{!! Form::open(array('url'=>'griev_reg_form','method'=>'POST', 'id'=>'myform')) !!}
<div class="box-body">
<div class="form-group">
<label class="col-sm-3 control-label">Grievance Case:
</label>
<label>
<input type="radio" name="gretype" class="minimal" checked>
</label>
<label>Normal Case
</label>
<label>
<input type="radio" name="gretype" class="minimal">
</label>
<label>
NRI
</label>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Grievance/Demand/Suggestion/Others:
</label>
<label>
<input type="radio" name="sugg_demand" class="minimal" checked value="G">
</label>
<label>Grievance
</label>
<label>
<input type="radio" name="sugg_demand" class="minimal" value="S">
</label>
<label>
Suggestion
</label>
<label>
<input type="radio" name="sugg_demand" class="minimal"
value="D">
</label>
<label>
Demand
</label>
<label>
<input type="radio" name="sugg_demand"
class="minimal" value="O">
</label>
<label>
Others
</label>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Department/Office:
</label>
<select class="form-control select2" name="dept_name" style="width: 28%;">
<option value="">Select Department</option>
<?php
foreach($departments as $result)
{
?>
<option value="<?php echo $result->deptcode; ?>"><?php echo $result->edesc; ?></option>
<?php
}
?>
</select>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Mobile No:</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-phone"></i>
</div>
<input type="text" class="form-control" name="mobileno">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Alternative Mobile No:</label>
<div class="input-group">
<div class="input-group-addon">
<i class="fa fa-phone"></i>
</div>
<input type="text" class="form-control" name="amobileno">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Received Date:</label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="text" class="form-control" name="recvd_date" id="datepicker" readonly="">
</div>
<!-- /.input group -->
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Name:</label>
<input type="text" class="form-control" name="cname">
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Individual or Group Complainant(s):
</label>
<label>
<input type="radio" name="indiv_grp" value="I" class="minimal" checked>
</label>
<label>Individual
</label>
<label>
<input type="radio" name="indiv_grp" value="G" class="minimal">
</label>
<label>Group
</label>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">AADHAR Card Number:</label>
<input type="text" class="form-control" name="idproofdetail">
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Address:</label>
<input type="text" class="form-control" name="address1">
</div>
<div class="form-group">
<input type="text" class="form-control box-right" name="address2">
</div>
<div class="form-group">
<input type="text" class="form-control box-right" name="address3">
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Pin code:</label>
<input type="text" class="form-control" name="pincode">
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Email:</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
<input type="email" class="form-control" placeholder="Email" name="email_address">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">District:
</label>
<select class="form-control select2" name="district_problem" style="width: 28%;">
</select>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">विधानसभा निर्वाचन क्षेत्र /Assembly Constituency:
</label>
<select class="form-control select2" name="ac_problem" style="width: 28%;">
</select>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Area:
</label>
<label>
<input type="radio" value="urban" name="problem_area" class="minimal" checked>
</label>
<label>Urban
</label>
<label>
<input type="radio" value="rural" name="problem_area" class="minimal">
</label>
<label>
Rural
</label>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Town/Block:
</label>
<select class="form-control select2" name="city_problem" style="width: 28%;">
</select>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Please Enter Specific Details about Your Grievance </label>
<textarea class="form-control" rows="3" name="Description" placeholder="Enter ..."></textarea>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">What do you want?</label>
<textarea class="form-control" rows="3" name="remedies" placeholder="Enter ..."></textarea>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Upload(Relevant Document):</label>
<input type="file" class="form-control">
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Captcha</label>
<label class="col-sm-3 control-label">
{!! app('captcha')->display(); !!}
</label>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Submit
</button>
</div>
</div>
</form>
when i click on submit button without filling any data, its doesn't go
on validation part
Please provide help how can i check the post value of input fileds as well as
captcha and also validate accordingly
I have checked your view..I didnt find any input field named as "name", you have "cname" so maybe you validation rule should be:
$rules = array(
'cname' => 'required',
'email_address' => 'required|email',
'g-recaptcha-response' => 'required|captcha'
);
In case you are using >=5.3 version then you should use:
public function postdata(Request $request)
{
//validation
$validationArray = [
'cname' => 'required',
'email_address' => 'required|email',
'g-recaptcha-response' => 'required|captcha'
];
$validator = Validator::make($request->all(),$validationArray);
if ($validator->fails()) {
$response = ['errors' => $validator->messages()->all()];
return Response::json($response,200);
}
//here if validation is successful
}
Ofcourse add at top of controller: use Illuminate\Http\Request;

Resources