How do I add multiple data at once in Laravel? - php

I have a field gender, age-group and patient_type. I want to store count for each patient type with age-group and gender. This is my form formData. And this is array of data I get arrayData. Also multiple age-group can be added.
For example, age-group=0-1, new_patient=>male=10,Female=60, old_patient=>male=50, female=20. How can I store this form data for multiple age-groups.
My Form code
<div class="newAgeGroup mt-5" id="newAgeGroup_0">
<div class="form-group row">
<div class="col-md-2">
<label for="">Select Age Group</label>
</div>
<div class="col-md-5 mb-3">
<select name="age_id[]" class="form-control" id="">
<option value="">Select Age Group</option>
#foreach($ages as $age)
<option value="{{$age->id}}">{{$age->start_age}}
-{{$age->end_age}}</option>
#endforeach
</select>
</div>
<div class="col-md-5">
<button type="button" onclick="addAgeGroup();"
class="btn btn-primary rounded-0 float-right addAgeGroup">Add Age Group
</button>
</div>
</div>
<table class="table table-bordered">
<thead>
<tr>
<th></th>
#foreach($patient_types as $patient_type)
<th>
{{$patient_type->name}}
<input type="hidden" name="patient_type_id[]" value="{{ $patient_type->id }}">
</th>
#endforeach
</tr>
</thead>
<tbody>
#foreach($genders as $gender)
<tr>
<th>
{{$gender->name}}
<input type="hidden" name="gender_id[]" value="{{ $gender->id }}">
</th>
#foreach($patient_types as $patient_type)
<?php
$name =
"number_".$patient_type->id."_".$gender->id ;
?>
<td>
<input type="text" name="{{ $name }}[]" class="form-control">
</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
</div>
And this is what I have tried.
foreach ($request->input('age_id') as $key => $value) {
foreach ($request->input('patient_type_id') as $pt => $ptvalue) {
foreach ($request->input('gender_id') as $gkey => $gvalue) {
foreach ($request->input('number_'.$ptvalue.'_'.$gvalue) as $nkey => $nvalue) {
$report = new Report();
$report->admin_id = $this->admin()->id;
$report->category_id = $request->center_type;
$report->center_id = $request->center_id;
$report->dateTime = $request->dateTime;
$report->nationality = $request->nationality;
$report->type = 'OPD';
$report->age_id = collect($request->get('age_id')[$key])->implode(',');
$report->patient_type_id = collect($request->get('patient_type_id')[$pt])->implode(',');
$report->gender_id = collect($request->get('gender_id')[$gkey])->implode(',');
$report->number = collect($request->get('number_'.$ptvalue.'_'.$gvalue)[$nkey])->implode(',');
$report->save();
}
}
}
}

If I understand well, you want a single age group to have multiple records associated with it.
If that's the case I recommend you, to use relation in the database to achieve that.
For example these two tables:
Age group
age group attributes
id
id
age
age_group_id
gender
patient type
And when you save, the relationship will have you have counters and a better way to unite everything.
I hope this is useful to you.

Related

Filter a form within another form for data submission

I'm working in Laravel 7.4. I have roles that correspond to two different "projects" or "areas", but they are all in the same table called "roles" and we can differentiate them with the "display_name" field, but I want my form to create a new User to show me the roles according to the "area" selected.
I've tried adding a condition in my controller and another form with the select, but since it's inside the form that submits the data to create the user, it's giving me trouble (the problem is that the data is sent as if the select were a submit button so it gives me the error that I have to fill in the name and email fields but I don't want that to happen).
What can I do?
This is my view
<form method="POST" action="{{route('User.store')}}">
{{ csrf_field() }}
#include('user.form', ['user'=> new App\User])
<!--Roles-->
<br>
<div class="row justify-content-center text-center">
<h3 class="txt-unam text-center text-primary">Roles</h3>
</div>
<div class="my-custom-scrollbar col-md-6">
<table class="table-scroll table table-sm table-striped ">
<thead class="txt-grey text-center">
<tr>
<th class="th-sticky bg-darker text-center">Role</th>
<th class="th-sticky bg-darker text-center">Select</th>
</tr>
</thead>
<tbody>
<form class="row justify-content-center " action="{{url('User')}}">
<select onchange="this.form.submit()" class="col-sm-6 col-12 form-control" name="id" id="selectRole">
<option selected value="" name="servescol">Servescol Roles</option>
<option selected value="" name="indicators">Indicators Roles</option>
</select>
</form>
#foreach($roles as $role)
<tr>
<td class="align-middle small text-center">{{$role->display_name}}</td>
<td class="align-middle text-center"><input type="checkbox" value="{{$role->id}}" id="checkIrRol" name="roles[]" {{$role->pluck('id')->contains($role->id) ? 'checked' : ''}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<br>
<div class="text-center">
<div class="text-center pt-0">
<button class="btn btn-success" type="submit" value="Save">Save</button>
<button class="btn btn-danger" onclick="cancel()" value="Cancel">Cancel</button>
</div>
</div>
</form>
This is my Controller
public function create(Request $request)
{
if ($request->get('servescol')) {
$roles = DB::select('SELECT id, role_name FROM roles WHERE id <= 6');
return view('user.create', compact('roles'));
}
if ($request->get('indicators')) {
$roles = DB::select('SELECT id, role_name FROM roles WHERE id > 6');
return view('user.create', compact('roles'));
}
$roles = Role::orderby('id')->get();
$departments = Department::all(['id', 'department_name']);
return view('user.create', compact('roles', 'departments'));
}
By the way, Departments is something different than "areas". The conditions <= 6 and > 6 correspond to the id according to the areas they belong to.
Using the query string, You could make something like
<tbody>
<tr>
<td colspan="2">
<select onchange="location.search = '?type=' + this.value">
<option selected disabled></option>
<option value="servescol">Servescol Roles</option>
<option value="indicators">Indicators Roles</option>
</select>
</td>
</tr>
</tbody>
if ($request->query('type') === 'servescol') {
$roles = DB::select('SELECT id, role_name FROM roles WHERE id <= 6');
return view('user.create', compact('roles'));
}
if ($request->query('type') === 'indicators') {
$roles = DB::select('SELECT id, role_name FROM roles WHERE id > 6');
return view('user.create', compact('roles'));
}

foreach() argument must be of type array|object, null given in laravel 8

I am trying to insert data by Select users from checkbox's and input different point for each user
but when I submit I got this error
and is that the correct way to insert multi value coming from checkboxes in the model
with different point for each user
Can someone please help me to find out Where I am wrong?
<form method="post" action="{{route('user.action.push')}}" >
#csrf
<table class="display table table-bordered table-separated" >
<thead>
<th scope="row">
<label class="custom-control custom-checkbox">
<input id="selectAll" class="custom-control-input" type="checkbox">
<span class="custom-control-label"></span>
<span style="background-color:red;" class="custom-control-description sr-only"></span>
</label>
</th>
<th>Name</th>
<th>Email</th>
<th>point</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td scope="row">
<label class="custom-control custom-checkbox">
<input type="checkbox" name="user_[{{$user->id}}]" value="{{$user->id}}" class="custom-control-input" >
<span class="custom-control-label"></span>
<span style="background-color:red;" class="custom-control-description sr-only"></span>
</label>
</td>
<td style="color:white">{{$user->name}}</td>
<td style="color:white">{{$user->email}}</td>
<td style="color:white"><input type="text" name="point_[{{$user->id}}]" >
</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th>Select</th>
<th>Name</th>
<th>Email</th>
<th>point</th>
</tr>
</tfoot>
</table>
</div>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
<div class="col-3">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">Select Action</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<div class="form-group">
<h5>Select Action <span class="text-danger">*</span></h5>
<div class="controls">
<select name="action_id" class="form-control" >
<option disabled="" selected="">Select Action</option>
#foreach($actions as $action)
<option value="{{$action->id}}">{{$action->action_title}}</option>
#endforeach
</select>
<br>
<button type="submit" class="btn btn-rounded btn-primary"> send</button>
</form>
the ActionUserPush function
public function ActionUserPush(Request $request){
foreach($request->user_id as $user_id){
$point = $request->point_[$user_id];
ActionUser::insert([
'user_id'=>$user_id,
'point' =>$point,
'action_id'=>$request->action_id,
]); }
$notification = array(
'message' => ' Action Activited Successfully',
'alert-type' => 'success'
);
return redirect()->route('user_action_view')->with($notification);}
You don't have any input with nameuser_idand in your form you can not define the input name dynamically like you have done. You should have to use a fixed name for it. Change your form input to:
<input type="checkbox" name="user_id[]" value="{{$user->id}}" class="custom-control-input" >
Then you can access it by using $request->user_id
You have to use like below
name="user_id[]"
You don't have any input with nameuser_idand in your form you can not define the input name dynamically like you have done. You should have to use a fixed name for it. Change your form input to:
id}}" class="custom-control-input" >
Then you can access it by using $request->user_id

how to search in a collection laravel?

i have 3 tables , arts(code,designation,prix,qte,gamme_id)
gammes(type)
factures(art_id,prix_total,quantité)
i want to search all gammes between 2 dates in facture table but the problem i dont have relation between factures and gammes , i just have relation between factures and arts
controller
public function store(Request $request)
{
$prix_totale=0;
$Gammes = Gamme::all();
$gamme_id=$request->get('gamme_id');
$date_debut=$request->get('date_debut');
$date_fin=$request->get('date_fin');
$articles=Art::where('gamme_id','LIKE',$gamme_id)->paginate(1000);
$data =Facture::where('art_id','LIKE',$articles)->whereBetween('created_at',[$date_debut,$date_fin])->orderByDesc('created_at')->paginate(1000);
foreach ($data as $p) {
$prix_totale+=$p->prix;
}
return view('affairegamme.resultat', compact('data','arts','prix_totale','articles'));
}
index.blade.php
<form method="post" action="{{ route('affairegamme.store') }}" enctype="multipart/form-data" width="20" height="20">
#csrf
<div class="form-group">
<label class="col-md-4">Gamme:</label>
<div class="col-md-8">
<select name="gamme_id" id="gamme_id" class="form-control" >
<option value="">Select Gamme</option>
#foreach($gammes as $gamme)
<option value="{{ $gamme->id}}">{{ $gamme->type }}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4">Date debut :</label>
<div class="col-md-8">
<input type="date" name="date_debut" value="{{ old('date', date('Y-m-d')) }}" class="form-control input-lg" />
</div>
</div>
<div class="form-group">
<label class="col-md-4">Date fin</label>
<div class="col-md-8">
<input type="date" name="date_fin" value="{{ old('date', date('Y-m-d')) }}" class="form-control input-lg" />
</div>
</div>
<br/>
<div class="form-group text-center">
<input type="submit" name="add" class="btn btn-primary input-lg" value="Chercher !" />
</div>
</form>
resultat.blade.php
<table class="table table-bordered table-striped">
<td><h6>ID de facture</h6></td>
<td><h6>date</h6></td>
<td><h6>Operation_id</h4></td>
<td><h6>Article</h4></td>
<td><h6>Prix Unitaire d Article</h4></td>
<td><h6>Quantite</h6></td>
<td><h6>Prix Totale</h6></td>
</tr>
#foreach($data as $row)
<tr>
<td>{{ $row->id}}</td>
<td>{{ $row->created_at}}</td>
<td>{{ $row->operation_id }}</td>
<td> {{$row->art->designation}}</td>
<td> {{$row->art->prix}}</td>
<td> {{$row->qte}}</td>
<td> {{$row->prix}}</td>
#endforeach
</table>
<br> </br>
<h1 style="font-size:200% ;text-align:center">Le chiffre d affaire pour la gamme est : {{$prix_totale}} dt</h1>
The below provided eloquent query will search for the fractures between the provided dates in the fracture table.
$grammesBetweenTwoDates = Gramme::whereHas('arts',function($query) use ($date_debut,$date_fin) {
// Any query to apply filter in arts
return $query->whereHas('fractures', function($fractureQuery) use ($date_debut,$date_fin){
// Any query to apply filter in fractures
return $fractureQuery->whereBetween('created_at', [$date_debut,$date_fin]);
});
})->paginate(1000);
Since you seem to have problem understanding the answer I will try my best to explain. The above query instructs the database to
Find all the grammes that have arts (from grames_id present in arts table ).
Find all the arts that have fractures (from arts_id present in fractures table )
Find all the fractures that are in between two dates ( from the date provided in eloquent query )
When above all cases match the query return the result.
In the above query you need to have arts relation in Grammes model.
public function arts()
{
return $this->hasMany(Arts::class);
}
And you need to gave fractures relation in Art model
public function fractures()
{
return $this->hasMany(Fracture::class);
}

Saving multiple table rows at once (Laravel 4)

I have a table where I get the data from database than i need to update all rows at once. Inside each cell I have added input fields. Now i want to be able to update all users at once when I enter the data but I dont know how.
Below is a picture of my view:
#extends('admin/master')
#section('content')
<section class="content">
{{Form::open()}}
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
<div class="col-lg-4">
<h3 class="box-title">INPUT EXAM RESULTS FOR EACH STUDENT: </h3>
</div>
<div class="pull-right col-lg-8">
<div class="col-lg-2 col-lg-offset-5 pull-left">
<select class="btn bg-navy" name="city" >
<option>Select City</option>
<option>Prishtin</option>
<option>Prizren</option>
</select>
</div>
<div class="col-lg-5 pull-right">
<div class="input-group">
<input type="text" name="search_input" class="form-control" placeholder="Search here...">
<div class="input-group-btn">
<button type="submit" class="btn btn-info"><i class="fa fa-search"></i> Search</button>
</div>
</div>
</div>
</div>
</div>
<div class="box-body">
<?php if (isset($data)) { ?>
<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th>{{Lang::get('messages.stid')}} </th>
<th>{{Lang::get('messages.name')}}</th>
<th>Subject 1</th>
<th>Subject 2</th>
<th>Subject 3</th>
<th>Subject 4</th>
</tr>
</thead>
<tbody>#foreach ($data as $row)
<tr>
<td>{{$row->id}}</td>
<td>{{$row->fname}} {{$row->lname}}</td>
<td><input type="text" name="sub1" class="form-control" placeholder="Add marks here..."></td>
<td><input type="text" name="sub2" class="form-control" placeholder="Add marks here..."></td>
<td><input type="text" name="sub3" class="form-control" placeholder="Add marks here..."></td>
<td><input type="text" name="sub4" class="form-control" placeholder="Add marks here..."></td>
</tr>
#endforeach
</tbody>
</table>
<button type="submit" class="col-lg-2 pull-right btn btn-success"><i class="fa fa-save"></i> Save</button>
<?php } ?>
</div>
</div>
</div>
</div>
{{Form::close()}}
</section>
{{ HTML::script('/admin/assets/js/jquery-2.2.3.min.js') }}
{{ HTML::script('/admin/assets/js/bootstrap.min.js') }}
{{ HTML::script('/admin/assets/js/jquery.dataTables.min.js') }}
{{ HTML::script('/admin/assets/js/dataTables.bootstrap.min.js') }}
<script>
$(function () {
$("#example1").DataTable();
$('#example2').DataTable({
"paging": true,
"lengthChange": false,
"searching": false,
"ordering": true,
"info": true,
"autoWidth": false
});
});
</script>
#stop
//Controller
public function search_input(){
$input = Input::get('search_input');
$city = Input::get('city');
$data = Apply::where('exam_venue', '=', "$input")
->where('city_applied', '=', "$city")->get();
if (isset($_POST['save'])) {
}
return View::make('admin/exam/edit',compact('data'));
}
Your input elements need to be arrays, keyed by $row->id (I assume this is the primary key of your students table) to identify which student that row represents. For example:
<td><input type="text" name="sub1[{{ $row->id}}]" class="form-control" placeholder="Add marks here..."></td>
Then when you submit the data, look up the student by the array key and update their information accordingly. Something to the effect of:
$key = // extract id from sub1 array
Student::find($request->input($key));
// do updates here
// repeat for each sub input...

How to get text from form and insert it into Database - Laravel 5.2

I am inserting a reduced trip price for my listing. In my form, I display all the trips for that listing. I don't know how to insert the already existing Trip name into the database though. I will show you what i mean.
( I'm not editing a trip, I'am creating a Promotional Listing, so the trip name and reduced price will go into a diffrent table )
I need to insert the "Trip" name on the left
This is how I have the form laid out:
<form class="form" method="post" action="{{ route('user.promotion-store') }}">
{{ csrf_field() }}
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Trip</th>
<th>Price</th>
<th>Reduced Price</th>
</tr>
</thead>
<tbody>
#foreach( $listings->trips as $trip )
<tr>
<td>
{{ $trip->name }}
</td>
<td>
${{ $trip->cost }}
</td>
<td>
<div class="col-md-12">
<div class="form-group{{ $errors->has('reduced_trip_price') ? ' has-error' : '' }}">
<label>Your Reduced Price</label>
<input type="text" class="form-control" name="reduced_trip_price" id="reduced_trip_price" placeholder="Optional...">
#if($errors->has('reduced_trip_price'))
<span class="help-block">{{ $errors->first('reduced_trip_price') }}</span>
#endif
</div>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
</form>
And this is how I insert it:
public function store(ReducedTripRequest $request) {
$reducedTrips = ReducedTrips($request->all());
$reducedTrips->save();
return redirect()->back();
}
Pass the Trip name within the form in a hidden input field this way:
<input type="hidden" name="trip_name" value="{{ $trip->name }}">
Then on the server side you can get the trip_name and reduced_trip_price to do whatever with them as you want.
To insert in a table for example:
<?php
NewTable::create([
'trip_name' => $request->input('trip_name'),
'reduced_trip_price' => $request->input('reduced_trip_price'),
]);

Categories