laravel foreach input radiobutton displayed incorrectly - php

I'm trying to display the options for the questions. i have the problem that when im doing it this way:
#section('content')
<div class="card">
<div class="card-header">Quiz: {{$category->name}}:</div>
<div class="card-body">
<form action="#" method="post" class="form-group">
#csrf
#foreach($questions as $key => $question)
<div class="form-group">
<label for="question">Question {{1+$key}}:</label>
<p class="card-text">{{$question->question_text}}</p>
#foreach($question->option_text as $key => $option)
<input type="radio">{{$option}}
#endforeach
</div>
#endforeach
<div class="form-group">
<input type="submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
i can check all radio buttons at the same time but if i put there a name for the radiobutton i can check only one of the options for the whole questions.. i think there's something strange with the foreach loop..
Info: the table "questions" has the following rows:
id, category_id, question_text, correct_answer, option_text (this is a json-field that is casted to an array)
Code from Controller:
public function store(Request $request, Category $category){
if($request->categoryTest == $category->name){
$questions = $category->question()->inRandomOrder()->get();
return view('user.test', compact('questions', 'category'));
}
}
do you have an idea how to fix this? thank you!

Try this:
#section('content')
<div class="card">
<div class="card-header">Quiz: {{$category->name}}:</div>
<div class="card-body">
<form action="#" method="post" class="form-group">
#csrf
#foreach($questions as $key => $question)
#php
$q = 1+$key
#endphp
<div class="form-group">
<label for="question">Question {{1+$key}}:</label>
<p class="card-text">{{$question->question_text}}</p>
#foreach($question->option_text as $key => $option)
<input name="radio-{{$q}}" type="radio">{{$option}}
#endforeach
</div>
#endforeach
<div class="form-group">
<input type="submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection

This seems to be a normal behavior of the radio buttons, only one is market at time. Have you tried to use checkbox instead?
Also, if you want to send it to backend, you'll need to set a name attribute. And here is the magic: instead of using a singular name, put it as array in order to get an array on your controller.
<input type="checkbox" name="question[]" class="btn btn-primary">

Related

How to store an array as single row for each value in my database

the whole idea is that when a user click on checkbox and save, the form should be submitted with an array containing the checked values, and save them in the database as single row for each value. i don't know if this is possible but i'm sure you guys have a solution or can help me?
and here's my form:
<div class="">
<form style="width: 70%" method="POST" action="{{ URL('/admin/projects/services/save') }}"
enctype="multipart/form-data">
#csrf
<input type="hidden" name="id" id="id" value="{{ #$id }}">
<input type="hidden" name="projectid" id="projectid" value="{{ #$projectid }}">
<div class="row topSpacer">
<div class="col-3">
<p class="bold">Selected Services: <span class="red">*</span></p>
</div>
<div class="col-9">
<input type="text" id="selectedServices" name="selectedServices" value="" readonly>
</div>
</div>
<div class="row topSpacer">
<div class="col-3">
<p class="bold">Select a service: <span class="red">*</span></p>
</div>
<div class="col-9">
#foreach ($services as $service)
<input type="checkbox" name="service" value="<?php echo stripslashes($service->id); ?>">
<label for="service"><?php echo stripslashes($service->name); ?></label>
<br>
<hr>
#endforeach
#error('service')
<span class="text-danger">{{ $message }}</span>
#enderror
</div>
</div>
<div class="row topSpacerHuge">
<div class="col-12 textRight">
<button type="button"
onclick="window.location='{{ url('admin/projects/services/index/'.$projectid) }}'"
class="goBackBtn">Cancel</button>
<input class="saveBtn" id="saveBtn" type="submit" value="Save">
</div>
</div>
</form>
</div>
and my script to get checkboxes value:
<script>
$(function() {
$("input:checkbox[name='service']").click(function() {
$value = [];
$.each($("input:checkbox[name='service']:checked"), function() {
$value.push($(this).val());
$array = $value.join();
});
// console.log(value.join(", "));
$("#selectedServices").val($array);
})
});
</script>
for implementing checked values I recommend to use the select tag
and you can give A name attribute with [] and it will collect all of your data
for example , look at my project's file which I'm trying to collect name of some users :
<select name="users[]" id="example-with-maxHeight" class="multiselect-group" multiple >
#foreach($users as $user)
<option #if($user->average < $averageParticipate )class="alert-danger" #endif value="{{$user->id}}">{{$user->name}} - {{$user->average}}</option>
#endforeach
</select>
have attention to users[] in name attribute
now in your request you can access all of your checked items .
if we look at your code ,can be some thing like this :
<label for="service"><?php echo stripslashes($service->name); ?></label>
<select name="services[]" id="" class="multiselect-group" multiple >
#foreach($users as $user)
<option value = "{{$sevice->id}}"> <?php echo stripslashes($service->name); ?></option>
#endforeach
</select>
now in your controllers you can access them :
request->get('services');
it will give you back an array of selected items .
Notice that id attribute helps me to interact with my java script code
you can rename it to yourselves .

Submitting individual entries from the same page -> with multiple forms

I have the following form:
#foreach($pleyers as $player)
<form action="/saveplayer" method="POST">
<div class="row">
<div class="col-6">
<p>{{$player['name']}}</p>
</div>
<div class="col-6">
<label>Score</label>
<input type="number" name="{{$player['id']}}"/>
</div>
</div>
</form>
#endforeach
MY FUNCTION
function savePlayer(Request $request){
}
How do I save the player scores individually, if the foreach generates 3 forms

How to fetch and update data in Laravel

So, I have two fields. In the first field I want to add old data and in the second field I want to add new data, and below those fields is a button which when clicked will fetch the data from Mongo Table and update the data inside.
I have tried implementing various logic but still no luck. Any sort of help would be appreciated.
This is my controller code:
public function updatedockets($dockets){
if (!Session::has('user_id')){
return Redirect::to('/login');
}
$cart = \App\Models\Cart::where('tracking_no',$dockets)->get()->toArray();
foreach ($cart as $value) {
$cart = \App\Models\Cart::where('tracking_no')->update(['tracking_no' => $dockets]);
}
}
This is my view code:
<div role="tabpanel" class="tab-pane" id="docket-replace">
<form method="post" id="frmDocket" action="/admin/viewdockets">
{!! csrf_field() !!}
<div>
<div class="form-group">
<textarea id="txtreploaceDockets" name="replacedockets" class="form-control" rows="15" placeholder="Enter old docket(s) here. Don't put more than 1000 docket numbers, all dockets should be comma(,) or new line separated"></textarea>
</div>
</div>
</form>
<form method="post" id="frmDocket" action="/admin/viewdockets">
{!! csrf_field() !!}
<div>
<div class="form-group">
<textarea id="txtreplaceDockets" name="replacedockets" class="form-control" rows="15" placeholder="Enter new docket(s) here. Don't put more than 1000 docket numbers, all dockets should be comma(,) or new line separated"></textarea>
</div>
<div class="form-group">
<div class="text-right pr-sm-15 pb-sm-15">
<button type="button" class="btn btn-primary btn-xs" id="btnReplaceDocketData" onclick="return confirm('Are you sure that you have entered the right docket number?')"> Replace Docket </button>
</div>
</div>
</div>
</form>
</div>
As you're not using $value, maybe below code helps you:
$cart = \App\Models\Cart::where('tracking_no', $dockets)
->update(['tracking_no' => $dockets]);
Sample:
$course->statuses()->updateOrCreate(
['status_id' => $id],
['status' => $request->get('status'), 'status_type' => Course::class]
);`

How to edit one column in a row of the database in laravel

How to edit one column in a row of the database in laravel
I can't update one column of row has multiple columns by laravel
My edit :
public function edit($id)
{
$addremark=bookappoitment::findOrFail($id);
return view('admin.ManageTime.addremarks', compact('addremark'));
}
My update:
public function update(Request $request, $id)
{
$request->validate([
'Remarks'=>'required'
]);
$data=bookappoitment::find($id);
$data->Remarks = $request->get('Remarks');
$data->save();
return view('/home');
}
link to update:
Update
form:
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Remarks :</label>
<textarea class="form-control" rows="10" name="Remarks" placeholder="Remarks">{{ $addremark->Remarks }}</textarea>
</div>
<a class="btn btn-success btn-mini deleteRecord " href="{{route('BookAppoint.update',$addremark->id)}}">Update</a>
</div>
</div>
</div>
You can update 1 (or more, just add to the array) column through the DB class.
DB::table('yourTable')->where('id', $id)->update(['remarks' => $request->input('Remarks')]);
You can also do it for a model like so:
bookappoitment::where('id', $id)->update(['remarks' => $request->input('Remarks')]);
It's in the Laravel documentation here.
<form class="" action="index.html" method="{{ route('BookAppoint.update', $addremark->id) }}">
#method('PATCH')
#csrf
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Remarks">Remarks :</label>
<textarea name="remarks" rows="10" placeholder="Remarks">{{$addremarks->Remarks}}</textarea>
</div>
</div>
<input type="submit" name="submit" value="Update">
</div>
</div>
</form>

Laravel - Form Action to Controller Function

I'm trying to create a situation where a user can load a page, select a timesheet number, and then have the system delete all records associated with that timesheet number. This function loads the form page:
public function deletetimesheetLoad()
{
$timesheets = collect(DB::select("SELECT dbo.TIME.Source AS Source
FROM dbo.TIME
GROUP BY dbo.TIME.Source
ORDER BY dbo.TIME.Source ASC"));
return view('utilities/deletetimesheet',['timesheets' => $timesheets]);
}
This is the actual blade template:
<form method="post"
action="{{url('/time/deletetimesheet/process')}}"
enctype="multipart/form-data"
<div class="form-group">
<label class="col-md-12 control-label" for="TIMESHEETNUMBER">Timesheet Number</label>
<div class="col-md-12">
<select required id="TIMESHEETNUMBER" name="TIMESHEETNUMBER" class="form-control select2_field">
<option value=""></option>
#foreach ($timesheets as $row)
<option value="{{ $row->Source }}">{{ $row->Source }}</option>
#endforeach
</select>
</div>
</div>
<div id="saveActions" class="form-group">
<input type="hidden" name="save_action" value="Submit">
<div class="btn-group">
<button type="submit" class="btn btn-success">
<span class="fa fa-save"></span>
<span data-value="Submit">Submit</span>
</button>
</div>
<span class="fa fa-ban"></span> Cancel
</div>
This is the function that the form action is pointing to:
public function deletetimesheetProcess(TimesheetRequest $request)
{
DB::table('TIME')->where('Source', $request->get('TIMESHEETNUMBER'))->delete();
\Alert::success(trans('yay'))->flash();
return view('details/customershow',[]);
}
Here are the defined routes for the two functions:
Route::get('/time/deletetimesheet', 'Admin\TimeCrudController#deletetimesheetLoad');
Route::post('/time/deletetimesheet/process', 'Admin\TimeCrudController#deletetimesheetProcess');
Currently the blade template loads correctly, and does not throw an error on submit - just reloads the current page. What am I doing wrong?

Categories