How to get sum score when checked the checkboxess? - ajax

I have score field in table of infractions. And I have many checkboxes. When I checked thed chechboxes, The total of scores are collected with ajax.
<div id="collapse6" class="panel-collapse collapse">
<div class="panel-body">
<div class="table-responsive">
<table class="table table-bordered">
#foreach($infractions as $infraction)
<tr>
<th>{{ $infraction->title }}</th>
<td>
<input type="hidden" name="infractions[{{ $infraction->id }}]" value="0" >
<input type="checkbox" value="1" onclick="{{ $infraction->score }}" name="infractions[{{ $infraction->id }}]" data-toggle="toggle">
</td>
</tr>
#endforeach
</table>
</div>
<div class="text-right"> Totlal
<span id="total">
</span>
</div>
</div>
</div>
Ajax
function scorePlus (id)
{
// var value = parseInt(document.getElementById('num'+id).value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('total').value = value;
}

Related

Laravel foreach total amount of all products of carts

I am creating carts in laravel. I want to display sum of all grandtotal as total but in total only last grandtotal is taken, is it incorrect logic?? i need help to getting total of all grandtotal.. is it any correction in code?? why $total takes only last grandtotal from database..
<div class="ps-cart-listing cart_page_empty">
<table class="table ps-cart__table">
<thead>
<tr>
<th>Products</th>
<th>Products Name</th>
<th>Price</th>
<th>Quantity</th>
<th>GrandTotal</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach($items as $item)
#php $subtotal="0" #endphp
<tr class="cartpage">
<td><a class="ps-product__preview" href="{{ route('productDetail',strtolower($item->product_slug))}}"><img class="mr-15" src="{{env('IMAGE_URL')}}/images/{{$item->product_single_image}}" alt="" height="100" width="100"></a></td>
<td>{{$item->product_name}}</td>
<td>{{$item->product_price}}</td>
<td>
<div class="form-group--number quantity" id="{{$item->cart_id}}">
<button class="quantity_minus"><span>-</span></button>
<input class="form-control cart_id" type="hidden" value="{{$item->cart_id}}">
<input class="form-control qty_input" type="text" value="{{$item->quantity}}">
<button class="quantity_plus"><span>+</span></button>
</div>
</td>
<td>{{$item->sub_total}}</td>
#php $subtotal = $subtotal + $item->sub_total #endphp
<td>
<div class="ps-remove"></div>
</td>
</tr>
#endforeach
</tbody>
</table>
<div class="ps-cart__actions">
<div class="ps-cart__promotion">
<div class="form-group">
<div class="ps-form--icon"><i class="fa fa-angle-right"></i>
<input class="form-control" type="text" placeholder="Promo Code">
</div>
</div>
<div class="form-group">
<button class="ps-btn ps-btn--gray">Continue Shopping</button>
</div>
</div>
<div class="ps-cart__total">
<h3>Sub Total:<span>{{$subtotal}} Rs.</span></h3>
<h3>Grand Total:<span>{{$subtotal}} Rs.</span></h3><a class="ps-btn" href="">checkout<i class="ps-icon-next"></i></a>
</div>
</div>
</div>
You need to assign $subtotal = 0; before the loop, otherwise on each loop it will count from 0 :
#php
$subtotal = 0;
#endphp
#foreach($items as $item)
#php
$subtotal = $subtotal + $item->sub_total
#endphp
//
#endforeach
Since you are not manipulating the values of $item->sub_total in the blade view, you can use the sum() collection method to get the grand total summing up all the sub_total values
No need to declare a $subtotal variable in the blade view and add the $item->sub_total individually to the $subtotal value.
$grandTotal = $items->sum('sub_total');
<div class="ps-cart__total">
<h3>Sub Total:<span>{{$subtotal}} Rs.</span></h3>
<h3>Grand Total:<span>{{$items->sum('sub_total')}} Rs.</span></h3><a class="ps-btn" href="">checkout<i class="ps-icon-next"></i></a>
</div>

Form Input Bindings v-model not rendering in a views page in laravel

I have question on it for the vue.js I already have a library vue.js in my web application site. Now my problem is when i try to use the Form Input Bindings the v-model this line of code here below
<input v-model="message" placeholder="edit me">
<p>Message is: {{ message }}</p>
what i notice is it says
Use of undefined constant test - assumed 'test'
So i try to see the solution on this one by adding a # beacuase laravel is using blade template. Now when i add a # the error is gone but the render goes this way {{ test }} . Why does it not rendered correctly in my browser?. Can someone help me figured this thing out?. Any help is muchly appreciated. TIA.
Here is my code
#extends('layouts.app')
#section('title', 'Cases New Invoice | RMTG')
#section('content')
<div id="content-wrapper">
<div class="container-fluid">
<!-- Breadcrumbs-->
<ol class="breadcrumb">
<li class="breadcrumb-item">
Dashboard
</li>
<li class="breadcrumb-item active">Cases New Invoice</li>
</ol>
<div class="row">
<div class="col-lg-12">
<div class="card mb-3">
<div class="card-header">
<i class="fa fa-file"></i>
New Invoice</div>
<div class="card-body">
<div class="form-group">
<div class="form-row">
<div class="col-md-6" >
<div class="table-responsive">
<form action="" method="post">
{{ csrf_field() }}
<table class="table table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th width="150px;">Contact Name: </th>
<td>{{ $opp->contacts }}</td>
</tr>
<tr>
<th>Case Number:</th>
<td>OPP -{{ $opp->code }}</td>
</tr>
<tr>
<th>Case Name:</th>
<td>{{ $opp->tax_year }}</td>
</tr>
<tr>
<th>Item Code:</th>
<td>
<div id="app-item">
<select name="itemCode" class="form-control">
<option v-for="item in items" v-bind:value="item.value">
#{{ item.text }}
</option>
</select>
</div>
</td>
</tr>
<tr>
<th>Notes: </th>
<td>
<input type="text" name="notes" class="form-control" />
</td>
</tr>
<tr>
<th>Amount: </th>
<td><input v-model="test" type="text" name="amount" class="form-control" required />
</td>
</tr>
<tr>
<th>VAT 20% Amt: </th>
<td>
<input type="text" name="vatAmount" class="form-control" />
</td>
</tr>
<tr>
<th>Total Amount: </th>
<td>
<input type="text" name="totalAmount" class="form-control" />
</td>
</tr>
</thead>
</table>
<div class="pull-right">
<button type="submit" class="btn btn-success">
<i class="fa fa-save" aria-hidden="true" style="font-size:24px"></i> Save
</button>
</div>
</form>
</div>
</div>
<div class="col-md-6" >
<h3>Amount Due: £ #{{ test }}</h3>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
//Item code
new Vue({
el: '#app-item',
data: {
items:[
{ text:'Tax Returns', value: 'Tax Returns' },
{ text:'UTR Filing', value: 'UTR Filing'}
]
}
})
</script>
#endsection

I have a problem with my laravel pagination

I am new to Laravel. I am trying to create pagination with the data in my bootstrap table.
This is my index Controller function:
$Courses=Courses::orderBy('name','desc')->paginate(1);
return view('Courses.index')->with('Courses',$Courses);
I am getting this error:
Call to undefined method App\Courses::links() (View:
C:\laragon\www\TharakaCollege\resources\views\Courses\index.blade.php)
The information provided is not enough, however I think the problem is in your view. You have to have a code like this in your blade file:
<div class="pages">
<ul class="pagination ">
<li>{{$Courses->appends(request()->query())->links()}}</li>
</ul>
</div>
Moreover you'd better edit your returning view like this:
return view('Courses.index', array('Courses'=> $Courses));
This is the code in Courses/index.blade.php
<div id="page-wrapper">
#include('includes.message');
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-lg-12">
<div class="panel panel-info">
<div class="panel-heading">
Courses
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="table-responsive">
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th class="text-center">#</th>
<th class="text-center"> code</th>
<th class="text-center">Course Name</th>
<th class="text-center">Category</th>
<th class="text-center">Requirements</th>
</tr>
</thead>
<tbody>
#if(count($Courses)>0)
#foreach($Courses as $Courses)
<tr class="item{{$Courses->id}}">
<td>{{$Courses->id}}</td>
<td>{{$Courses->code}}</td>
<td>{{$Courses->name}}</td>
<td>{{$Courses->category}}</td>
<td>{{$Courses->requirements}}</td>
<td><a class="btn btn-info btn-sm" href="/Courses/{{$Courses->id}}/edit">Edit</a>
</td>
<td>
<form method="POST" action="{{route('Courses.destroy',$Courses->id)}}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" class="btn btn-danger btn-sm" value="Delete">
</form>
</td>
</tr>
#endforeach
#else
<p>No Courses</p>
#endif
</tbody>
</table>
{{ $Courses->links() }}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
You should try this:
<div class="pull-left">
<div class="row">
<div class="col-xs-12">
<div class="explore-pagination">
<nav>
<div class="pagination"> {{ $Courses->render() }}</div>
</nav>
</div>
</div>
</div>
</div>
Try this code instead (modified version of yours):
<div id="page-wrapper">
#include('includes.message');
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-lg-12">
<div class="panel panel-info">
<div class="panel-heading">
Courses
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="table-responsive">
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th class="text-center">#</th>
<th class="text-center"> code</th>
<th class="text-center">Course Name</th>
<th class="text-center">Category</th>
<th class="text-center">Requirements</th>
</tr>
</thead>
<tbody>
#if(count($Courses)>0)
#foreach($Courses as $Course)
<tr class="item{{$Course->id}}">
<td>{{$Course->id}}</td>
<td>{{$Course->code}}</td>
<td>{{$Course->name}}</td>
<td>{{$Course->category}}</td>
<td>{{$Course->requirements}}</td>
<td><a class="btn btn-info btn-sm" href="/Courses/{{$Course->id}}/edit">Edit</a>
</td>
<td>
<form method="POST" action="{{route('Courses.destroy',$Course->id)}}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" class="btn btn-danger btn-sm" value="Delete">
</form>
</td>
</tr>
#endforeach
#else
<p>No Courses</p>
#endif
</tbody>
</table>
{{$Courses->appends(request()->query())->links()}}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
I have modified your foreach loop as well as ->links().
When calling the paginate method, you will receive an instance of Illuminate\Pagination\LengthAwarePaginator. In addition to these helpers methods, the paginator instances are iterators and may be looped as an array. So, once you have retrieved the results, you may display the results and render the page links using Blade:
Your blade should contain:
<div class="container">
#foreach ($courses as $course)
{{ $course->fieldName }}
#endforeach
</div>
{{ $courses->links() }}

sum score in ajax not working with laravel5.4

I have score field in table of infractions. And I have many checkboxes. When I checked the chechboxes, The total of scores are collected with ajax.
<div class="panel-body">
<div class="table-responsive">
<table class="table table-bordered">
#foreach($infractions as $infraction)
<tr>
<th>{{ $infraction->title }}</th>
<td>
<input type="checkbox" data-toggle="toggle" data-on="Yes" data-off="No" data-onstyle="success" data-offstyle="danger" data-score="{{ $infraction->score }}" onClick="updateTotal();">
</td>
</tr>
#endforeach
</table>
</div>
<div class="text-right">Total:
<span id="total">
</span>
</div>
</div>
Ajax
function scorePlus (id)
{
var total = id + id;
document.getElementById('total').value = total;
}
This should work
<div class="panel-body">
<div class="table-responsive">
<table class="table table-bordered">
#foreach($infractions as $infraction)
<tr>
<th>{{ $infraction->title }}</th>
<td>
<input type="checkbox" data-score="{{ $infraction->score }})" onClick="updateTotal();">
</td>
</tr>
#endforeach
</table>
</div>
<div class="text-right">Total:
<span id="total">0</span>
</div>
</div>
<script>
function updateTotal() {
var checked = document.querySelectorAll('input[type=checkbox]:checked');
var score = 0;
Array.prototype.forEach.call(checked, function(el, i){
score += parseInt(el.getAttribute('data-score'));
});
document.getElementById('total').innerHTML = score;
}
</script>

In laravel data is not inserting into database

I am new to laravel and doing project using laravel framework please help me to solve this problem.
data is not inserting into table.
routes.php
Route::post('report/save','TestingController#store');
TestingController
public function store(Request $request){
$userId = \Auth::user()->id;
$this->validate($request, [
'from_stk_loc' => 'required',
'testing_date' => 'required',
'casting_date' => 'required',
'debtor_no' => 'required',
'concrete_grade' => 'required',
'testing_age' => 'required',
]);
$test_details['client_id'] = $request->debtor_no;
$test_details['location'] = $request->from_stk_loc;
$test_details['casting_date'] = $request->casting_date;
$test_details['testing_date'] = $request->testing_date;
$test_details['concrete_grade'] = $request->concrete_grade;
$test_details['testing_age'] = $request->testing_age;
$test_details['report_date'] = date('Y-m-d');
$test_detailsId = DB::table('testing_report')->insert($test_details);
}
Model-> Testing.php
<?php
namespace App\Model;
use DB;
use Illuminate\Database\Eloquent\Model;
class Testing extends Model
{
protected $table = 'testing_report';
protected $fillable = [
'client_id',
'location',
'casting_date',
'testing_date',
'concrete_grade',
'testing_age',
];
}
Here is my form code, you can review this and find me a solution.
<div class="col-md-3">
<label for="exampleInputEmail1">{{ trans('message.testing.client') }}</label>
<select class="form-control select2" name="client_id" id="client_id">
<option value="all">Select client</option>
#foreach($customerData as $data)
<option value="{{$data->debtor_no}}">{{$data->name}}</option>
#endforeach
</select>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="examplefromlocation">{{ trans('message.form.from_location') }}</label>
<select class="form-control select2" name="from_stk_loc" id="from_stk_loc">
#foreach($locData as $data)
<option value="{{$data->loc_code}}" <?= ($data->inactive =="1" ? 'selected':'')?>>{{$data->location_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>{{ trans('message.table.casting_date') }}<span class="text-danger"> *</span></label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input class="form-control" id="datepicker" type="text" name="casting_date">
</div>
<!-- /.input group -->
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>{{ trans('message.table.testing_date') }}<span class="text-danger"> *</span></label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input class="form-control" id="datepicker1" type="text" name="testing_date">
</div>
<!-- /.input group -->
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="examplefromlocation">{{ trans('message.form.gradeofconcrete') }}</label>
<input type="text" placeholder="{{ trans('message.form.gradeofconcrete') }}" class="form-control valdation_check" name="concrete_grade" value="">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="examplefromlocation">{{ trans('message.form.testing_age') }}</label>
<input type="text" placeholder="{{ trans('message.form.testing_age') }}" class="form-control valdation_check" name="testing_age" value="">
</div>
</div>
<div class="row">
<div class="col-md-12">
<!-- /.box-header -->
<div class="box-body no-padding">
<div class="table-responsive">
<table class="table table-bordered" id="purchaseInvoice">
<tbody>
<tr class="tbl_header_color dynamicRows">
<th width="5%" class="text-center">{{ trans('message.table.sno') }}</th>
<th width="10%" class="text-center">{{ trans('message.table.wt') }}</th>
<th colspan="2" width="10%" class="text-center">{{ trans('message.table.load') }}</th>
<th colspan="2" width="10%" class="text-center">Comp Strength in M. Pa</th>
<th width="15%" class="text-center">Avg Strength</th>
<th width="15%" class="text-center">{{ trans('message.table.remark') }}</th>
<th width="5%" class="text-center">{{ trans('message.table.action') }}</th>
</tr>
<tr class="tbl_header_color dynamicRows">
<th width="5%" class="text-center"></th>
<th width="10%" class="text-center"></th>
<th width="10%" class="text-center">{{ trans('message.table.machine') }}</th>
<th width="10%" class="text-center">{{ trans('message.table.calibrated') }}</th>
<th width="10%" class="text-center">7 Day's</th>
<th width="10%" class="text-center">27 Day's</th>
<th width="10%" class="text-center"></th>
<th width="15%" class="text-center"></th>
<th width="5%" class="text-center"></th>
</tr>
<tr class="custom-item"><td class="add-row text-danger"><strong>Add Custom Item</strong></td><td colspan="8"></td></tr>
</tbody>
</table>
</div>
<br><br>
</div>
</div>
<!-- /.box-body -->
<div class="col-md-12">
{{ trans('message.form.cancel') }}
<button type="submit" class="btn btn-primary btn-flat pull-right" id="btnSubmit">{{ trans('message.form.submit') }}</button>
</div>
</div>
</form>
Problem:
When I click submit button nothing is happening at all.
Any help would be highly appreciated.
This is view
<div class="col-md-3">
<label for="exampleInputEmail1">{{ trans('message.testing.client') }}</label>
<select class="form-control select2" name="client_id" id="client_id">
<option value="all">Select client</option>
#foreach($customerData as $data)
<option value="{{$data->debtor_no}}">{{$data->name}}</option>
#endforeach
</select>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="examplefromlocation">{{ trans('message.form.from_location') }}</label>
<select class="form-control select2" name="from_stk_loc" id="from_stk_loc">
#foreach($locData as $data)
<option value="{{$data->loc_code}}" <?= ($data->inactive =="1" ? 'selected':'')?>>{{$data->location_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>{{ trans('message.table.casting_date') }}<span class="text-danger"> *</span></label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input class="form-control" id="datepicker" type="text" name="casting_date">
</div>
<!-- /.input group -->
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>{{ trans('message.table.testing_date') }}<span class="text-danger"> *</span></label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input class="form-control" id="datepicker1" type="text" name="testing_date">
</div>
<!-- /.input group -->
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="examplefromlocation">{{ trans('message.form.gradeofconcrete') }}</label>
<input type="text" placeholder="{{ trans('message.form.gradeofconcrete') }}" class="form-control valdation_check" name="concrete_grade" value="">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="examplefromlocation">{{ trans('message.form.testing_age') }}</label>
<input type="text" placeholder="{{ trans('message.form.testing_age') }}" class="form-control valdation_check" name="testing_age" value="">
</div>
</div>
<div class="row">
<div class="col-md-12">
<!-- /.box-header -->
<div class="box-body no-padding">
<div class="table-responsive">
<table class="table table-bordered" id="purchaseInvoice">
<tbody>
<tr class="tbl_header_color dynamicRows">
<th width="5%" class="text-center">{{ trans('message.table.sno') }}</th>
<th width="10%" class="text-center">{{ trans('message.table.wt') }}</th>
<th colspan="2" width="10%" class="text-center">{{ trans('message.table.load') }}</th>
<th colspan="2" width="10%" class="text-center">Comp Strength in M. Pa</th>
<th width="15%" class="text-center">Avg Strength</th>
<th width="15%" class="text-center">{{ trans('message.table.remark') }}</th>
<th width="5%" class="text-center">{{ trans('message.table.action') }}</th>
</tr>
<tr class="tbl_header_color dynamicRows">
<th width="5%" class="text-center"></th>
<th width="10%" class="text-center"></th>
<th width="10%" class="text-center">{{ trans('message.table.machine') }}</th>
<th width="10%" class="text-center">{{ trans('message.table.calibrated') }}</th>
<th width="10%" class="text-center">7 Day's</th>
<th width="10%" class="text-center">27 Day's</th>
<th width="10%" class="text-center"></th>
<th width="15%" class="text-center"></th>
<th width="5%" class="text-center"></th>
</tr>
<tr class="custom-item"><td class="add-row text-danger"><strong>Add Custom Item</strong></td><td colspan="8"></td></tr>
</tbody>
</table>
</div>
<br><br>
</div>
</div>
<!-- /.box-body -->
<div class="col-md-12">
{{ trans('message.form.cancel') }}
<button type="submit" class="btn btn-primary btn-flat pull-right" id="btnSubmit">{{ trans('message.form.submit') }}</button>
</div>
</div>
</form>
To store data I would give your html elements name as db field and use simple:
$testing = new Testing();
$testing->fill($request->all());
$testing->client_id = $request->your_client_id_from_source;
$testing->user_id = auth()->user()->id;
$testing->save();
So you will use directly laravel ORM and not raw db queries.
Your Model can only be used if your using Eloquent Queries. So based on your code your not using the model because your using Query Builder
I have 2 suggestions:
Check your date format "date('Y-m-d')" make sure that mysql allow this.
Change your $request->from_stk_loc to $request['from_stk_loc'] and the rest if the codes.
Try again, let me know if that helps.
First of all, I agree with everyone recommending Eloquent's ORM functionality over 'raw' DB queries.
As for a possible explanation: you are trying to insert into a non-fillable column: 'report_date', which might not be nullable in this table.
So try this in your model:
protected $fillable = [
'client_id',
'location',
'casting_date',
'testing_date',
'concrete_grade',
'testing_age',
'report_date'
];
Try this:
use App\Model\Testing;
public function store(Request $request){
$userId = \Auth::user()->id;
$validator = Validator::make($request->all(), [
'from_stk_loc' => 'required',
'testing_date' => 'required',
'casting_date' => 'required',
'debtor_no' => 'required',
'concrete_grade' => 'required',
'testing_age' => 'required',
]);
if( $validator->fails() )
{
return redirect()->back()->withInput()->withErrors($validator);
} else {
$testingObj = new Testing;
$testingObj->client_id = $request->client_id;
$testingObj->location = $request->location;
$testingObj->casting_date = $request->casting_date;
$testingObj->testing_date = $request->testing_date;
$testingObj->concrete_grade = $request->concrete_grade;
$testingObj->testing_age = $request->testing_age;
$testingObj->testing_age = $request->testing_age;
$testingObj->report_date = date('Y-m-d'); // try date('Y-m-d H:i:s') if datatype is datetime
$testingObj->save();
$test_detailsId = $testingObj->id;
}
Finally solved this problem by adding <input type="hidden" value="{{csrf_token()}}" name="_token" id="token"> in view page
please make sure in model fillable fields are respective field is mentioned ,correct with no extra space accurately. In your model you have not mentioned report_date field.
namespace App\Model;
use DB;
use Illuminate\Database\Eloquent\Model;
class Testing extends Model
{
protected $table = 'testing_report';
protected $fillable
= [
'client_id',
'location',
'casting_date',
'testing_date',
'concrete_grade',
'testing_age',
//add this to the end.
'report_date'
];

Resources