I want to make an invoice system with laravel using vuejs by api. Firstly show all product and when i click add to cart it added.
Here is the screenshot:
Now i want to send input fill data to order_masters table and including last inserted ID cart data send order_details table.
but i cant not pass the whole data. sometime pass customer information but not cart data.
N. B: I am using vuejs, vform and API
My component code is given below:
<template>
<div class="row">
<div class="col-md-7">
<div class="col-md-11">
<input type="hidden" v-model="field">
<input type="search" v-model="query" class="form-control form-control-sm mb-3" placeholder="Search Here">
</div>
<div class="row justify-content-center">
<div v-show="products.length" v-for="(product, id) in products" :key="id" class="card col-xl-3 col-md-3 col-sm-3 mb-5 mr-5 float-left" style="background-color:lightgray">
<div class="card-body">
<h5 class="card-title">{{product.name}}</h5>
<p class="card-text"><input type="text" v-model="product.qty" class="form-control form-control-sm mb-2"></p>
<strong class="">
{{product.price}} TK
</strong>
<button class="btn btn-sm btn-primary float-right" #click="addToCart(product)">
<i class="fas fa-plus"></i>
</button>
</div>
</div>
</div>
<div v-show="!products.length" class="alert alert-danger text-center" role="alert">
<strong>Opps! No Data Found!</strong>
</div>
<pagination v-if="pagination.last_page > 1" :pagination="pagination" :offset="5" #paginate="query === ''? showProduct(): searchProduct()"></pagination>
</div>
<div class="col-md-5 float-righ">
<form #submit.prevent="store" #keydown="form.onKeydown($event)">
<alert-error :form="form"></alert-error>
<div class="row mb-2">
<label for="" class="col-md-3 font-weight-bold">Name</label>
<div class="col-md-9">
<input type="text" v-model="form.name" name="name" class="form-control form-control-sm" :class="{ 'is-invalid': form.errors.has('name') }">
<has-error :form="form" field="name"></has-error>
</div>
</div>
<div class="row mb-2">
<label for="" class="col-md-3 font-weight-bold">Phone</label>
<div class="col-md-9">
<input type="text" v-model="form.phone" name="phone" class="form-control form-control-sm" :class="{ 'is-invalid': form.errors.has('phone') }">
<has-error :form="form" field="phone"></has-error>
</div>
</div>
<div class="row mb-2">
<label for="" class="col-md-3 font-weight-bold">Address</label>
<div class="col-md-9">
<input type="text" v-model="form.address" name="address" class="form-control form-control-sm" :class="{ 'is-invalid': form.errors.has('address') }" >
<has-error :form="form" field="address"></has-error>
</div>
</div>
<div class="mb-5">
<input type="button" class="btn btn-sm btn-danger float-left" #click="clearCart" value="Clear" />
<button type="submit" :disabled="form.busy" class="btn btn-sm btn-success float-right" #click="store">Checkout</button>
</div>
<table class="table table-sm table-bordered table-striped">
<thead class="text-center">
<tr>
<th>ID</th>
<th>Name</th>
<th>Qty</th>
<th>Price</th>
<th>L/T</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr v-for="(cart, i) in carts" :key="i">
<td class="text-center"><input type="hidden" name="pro_id[]" v-model="form.pro_id"> {{cart.id}}</td>
<td>{{cart.name}} </td>
<td class="text-right" name="qty"><input type="hidden" name="qty[]" v-model="form.qty">{{cart.qty}}</td>
<td class="text-right" name="unit_price"><input type="hidden" name="pro_id[]" v-model="form.price">{{cart.price}}</td>
<td class="text-right">{{cart.price*cart.qty}}</td>
<td class="text-center"><button type="button" #click="removeProduct(i)" class="btn btn-sm btn-danger">x</button></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="4" class="text-right font-weight-bold">Total =</td>
<td class="text-right"> {{total}}/-</td>
</tr>
</tfoot>
</table>
</form>
</div>
<vue-progress-bar></vue-progress-bar>
<vue-snotify></vue-snotify>
</div>
<script>
export default {
data(){
return{
field: 'name',
query: '',
form: new Form({
'name': '',
'phone': '',
'address':'',
'pro_id' : [],
'qty' : [],
'unit_price' : []
}),
products:[],
carts:[],
pagination:{
current_page: 1
}
}
},
methods:{
store(){
var data = {
carts: this.carts,
}
this.$Progress.start()
this.form.busy = true
this.form.post('api/pos', data)
.then( res => {
this.form.reset()
this.form.clear()
this.$Progress.finish()
console.log(data)
})
.catch( e => {
this.$Progress.fail()
console.log(e)
})
}
}
}
</script>
Here is API Controller Function:
public function store(Request $request)
{
$carts = json_decode($request->getContent('data'), true);
$this->validate($request,[
'name' => 'required',
'phone' => 'required',
'address' => 'required'
]);
$order = new OrderMaster;
$order->name = $request->name;
$order->phone = $request->phone;
$order->address = $request->address;
$order->save();
$order_id = DB::getPdo()->lastInsertId();
foreach ($carts as $cart) {
OrderDetails::create([
'order_id' => $order_id,
'product_id' => ?,
'qty' => ?,
'unite_price' => ?,
]);
}
}
First, you're calling form.post wrong. Second parameter is not additional data, but config options for axios.request call.
In order to send carts array you should add it to the form itself:
form: new Form({
'name': '',
'phone': '',
'address':'',
'pro_id' : [],
'qty' : [],
'unit_price' : [],
'carts' : [], // <-- added
}),
Then, replace usage of carts with form.carts in your template (and methods).
Now, carts will be sent to the server along with other data.
And lastly, you don't need to use json_decode at all.
//...
$data = $this->validate($request, [
'name' => 'required',
'phone' => 'required',
'address' => 'required',
'carts' => 'required|array',
'carts.*.name' => 'string',
'carts.*.id' => 'integer',
'carts.*.qty' => 'integer',
'carts.*.price' => 'numeric',
]);
Then, you can use $data as a source of data.
foreach ($data['carts'] as $cart) {
OrderDetails::create([
'order_id' => $order_id,
'product_id' => $cart['id'],
'qty' => $cart['qty'],
'unite_price' => $cart['price'],
]);
}
I had been done it in other way. Not using vform i used axios. but i want to done it using vform.
return{
field: 'name',
query: '',
form:{
'name': '',
'phone': '',
'address':''
},
products:[],
carts:[],
pagination:{
current_page: 1
}
}
store(){
let client = {
cli: this.form
}
let data = {
carts: this.carts
}
this.$Progress.start()
axios.post('api/pos', {data, client})
.then( res => {
this.$Progress.finish()
console.log(res)
})
.catch( e => {
this.$Progress.fail()
console.log(e)
})
}
Controller:
$carts = json_decode($request->getContent('client','data'), true);
$customer = $carts['client'];
$pro_info = $carts['data'];
foreach ($customer as $cus) {
$order = new OrderMaster;
$order->name = $cus['name'];
$order->phone = $cus['phone'];
$order->address = $cus['address'];
$order->save();
}
$order_id = DB::getPdo()->lastInsertId();
foreach ($pro_info as $cart) {
foreach ($cart as $c) {
OrderDetails::create([
'order_id' => $order_id,
'product_id' => $c['id'],
'qty' => $c['qty'],
'unit_price' => $c['price'],
]);
}
}
Related
I have this code in Laravel-5.8. There are two models:
class HrLeaveType extends Model
{
protected $table = 'hr_leave_types';
protected $primaryKey = 'id';
protected $fillable = [
'leave_type_name',
'leave_type_code',
'description',
];
public function leavetypedetail()
{
return $this->hasMany('App\Models\Hr\HrLeaveTypeDetail');
}
}
class HrLeaveTypeDetail extends Model
{
protected $table = 'hr_leave_type_details';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'leave_type_id',
'company_id',
'employee_type_id',
'no_of_days',
];
protected $casts = [
'data' => 'array',
];
public function leavetype()
{
return $this->belongsTo('App\Models\Hr\HrLeaveType', 'leave_type_id', 'id');
}
public function employeetype()
{
return $this->belongsTo('App\Models\Hr\HrEmployeeType', 'employee_type_id', 'id' );
}
}
Request Rules
public function rules()
{
return [
'leave_type_name' => [
'required',
'string',
'min:3',
'max:80',
Rule::unique('hr_leave_types')->where(function ($query) {
return $query->where('leave_type_name', $this->leave_type_name)
->where('company_id', $this->company_id);
})
],
'leave_type_code' => [
'nullable',
'string',
'max:10',
Rule::unique('hr_leave_types')->where(function ($query) {
return $query->where('leave_type_code', $this->leave_type_code)
->where('company_id', $this->company_id);
})
],
'no_of_days' => 'required|array',
'no_of_days.*' => [
'required',
'numeric',
'max:120'
],
'employee_type_id' => 'required|array',
'employee_type_id.*' => [
'required',
],
];
}
}
Controller
public function create()
{
$userCompany = Auth::user()->company_id;
$employeetypes = HrEmployeeType::where('company_id', $userCompany)->get();
$leavetype = new HrLeaveType();
return view('leave.leave_types.create')
->with('leavetype', $leavetype)
->with('employeetypes', $employeetypes);
}
public function store(StoreLeaveTypeRequest $request)
{
$userCompany = Auth::user()->company_id;
$leavetype = new HrLeaveType();
$leavetype->leave_type_name = $request->leave_type_name;
$leavetype->leave_type_code = $request->leave_type_code;
$leavetype->description = $request->description;
$leavetype->save();
foreach ($request->employee_type_id as $key => $employee_type_id){
$insert_array = [
'no_of_days' => $request->no_of_days[$key],
'employee_type_id' => $request->employee_type_id[$key],
'leave_type_id' => $leavetype->id,
];
HrLeaveTypeDetail::create($insert_array );
}
Session::flash('success', 'Leave Type is created successfully');
return redirect()->route('leave.leave_types.index');
}
}
LeaveTypeDetail is dynamically created
create.blade
#if (Session::has('error'))
<div class="alert alert-warning" align="left">
×
<strong>!</strong> {{Session::get('error')}}
</div>
#endif
<br>
#include('partials._messages')
<form action="{{route('leave.leave_types.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<div class="card-body">
<div class="form-body">
<div class="row">
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Leave Type Name:<span style="color:red;">*</span></label>
<input type="text" name="leave_type_name" value="{{ old('leave_type_name', $leavetype->leave_type_name) }}" placeholder="Enter leave type name" class="form-control #error('leave_type_name') is-invalid #enderror">
#error('leave_type_name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Leave Type Code:</label>
<input type="text" name="leave_type_code" value="{{ old('leave_type_code', $leavetype->leave_type_code) }}" placeholder="Enter leave typecode" class="form-control #error('leave_type_code') is-invalid #enderror">
#error('leave_type_code')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label>Description</label>
<textarea rows="2" name="description" class="form-control #error('description') is-invalid #enderror" value="{{old('description',$leavetype->description)}}" placeholder="Enter Description here ...">{{old('description',$leavetype->description)}}</textarea>
#error('description')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-sm-12">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Employee Type<span style="color:red;">*</span></th>
<th scope="col">Leave Days<span style="color:red;">*</span></th>
<th scope="col"><a class="btn btn-info addRow"><i class="fa fa-plus"></i></a></th>
</tr>
</thead>
<tbody>
<tr>
<td width="60%">
<option value="0" selected="true" disabled="true">Select Employee Type</option>
#if($employeetypes->count() > 0 )
#foreach($employeetypes as $employeetype)
<option name="employee_type_id[]" value="{{$employeetype->id}}">{{$employeetype->employee_type_name}}</option>
#endforeach
#endif
</select>
</td>
<td width="35%"><input type="text" name="no_of_days[]" placeholder="Enter leave days here" class="form-control no_of_days" max="120"></td>
<td width="5%"><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
<button type="button" onclick="window.location.href='{{route('leave.leave_types.index')}}'" class="btn btn-default">Cancel</button>
</div>
</form>
<script type="text/javascript">
$(document).ready(function(){
$('.addRow').on('click', function () {
addRow();
});
function addRow() {
var addRow = '<tr>\n' +
' <td width="60%"><select class="form-control select2bs4" data-placeholder="Choose Employee Type" tabindex="1" name="employee_type_id[]">\n' +
' <option value="0" selected="true" disabled="true">Select Employee Type</option>\n' +
' #if($employeetypes->count() > 0 )\n' +
' #foreach($employeetypes as $employeetype)\n' +
' <option value="{{$employeetype->id}}">{{$employeetype->employee_type_name}}</option>\n' +
' #endforeach\n' +
' #endif\n' +
' </select></td>\n' +
' <td width="35%"><input type="text" name="no_of_days[]" placeholder="Enter leave days here" class="form-control no_of_days" max="120"></td>\n' +
' <td width="5%"><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>\n' +
' </tr>';
$('tbody').append(addRow);
addRemoveListener();
};
addRemoveListener();
});
</script>
One leave type has many leave type details. For the controller and view, I have dynamic form input.
leave_type_details are arrays.
When user submits, if action fails all the fields should retail their values. This happens to only leave_type, but leave_type_details fields did not retain their values. It clears off.
How do I achieve this?
Thanks
in blade :
#if(old('input_name'))
#foreach(old('input_name') as $key => $value)
<your_html>{{old("input_name.{$key}"}}</your_html>
#endforeach
#endif
I am trying to create Dynamic input form array using Laravel-5.8
I have these two models:
class HrLeaveType extends Model
{
protected $table = 'hr_leave_types';
protected $fillable = [
'company_id',
'leave_type_name',
'no_of_days',
'leave_type_code',
];
}
class HrLeaveTypeDetail extends Model
{
protected $table = 'hr_leave_type_details';
protected $fillable = [
'id',
'leave_type_id',
'company_id',
'employment_type_code',
'no_of_days',
];
public function leavetype()
{
return $this->belongsTo('App\Models\Hr\HrLeaveType', 'leave_type_id', 'id');
}
public function employmenttype()
{
return $this->belongsTo('App\Models\Hr\HrEmploymentType', 'employment_type_code', 'employment_type_code' );
}
}
Request Rules:
class UpdateLeaveTypeRequest extends FormRequest
{
public function rules()
{
'leave_type_name' =>
[
'required',
Rule::unique('hr_leave_types')->where(function ($query) {
return $query
->where('leave_type_name', 1)
->where('company_id', 1);
})->ignore($this->leave_type)
],
'leave_type_code' => [
'nullable',
'string',
'max:10',
],
'no_of_days' => 'required|array',
'no_of_days.*' => [
'required',
'numeric',
'max:120'
],
'employment_type_code' => 'required|array',
'employment_type_code.*' => [
'required',
],
];
}
}
And then the Controller:
public function edit($id)
{
$userCompany = Auth::user()->company_id;
$leavetype = HrLeaveType::findOrFail($id);
$employmenttypes = HrEmploymentType::where('company_id', $userCompany)->get();
$leavetypedetails = HrLeaveTypeDetail::where('leave_type_id', $id)->get();
return view('leave.leave_types.edit')
->with('leavetype', $leavetype)
->with('leavetypedetails', $leavetypedetails)
->with('employmenttypes', $employmenttypes);
}
public function update(UpdateLeaveTypeRequest $request, $id)
{
DB::beginTransaction();
try {
$leavetype = HrLeaveType::findOrFail($id);
$leavetype = new HrLeaveType();
$leavetype->leave_type_name = $request->leave_type_name;
$leavetype->leave_type_code = $request->leave_type_code;
$leavetype->description = $request->description;
$leavetype->save();
HrLeaveTypeDetail::where('leave_type_id', $id)->delete();
foreach ( $request->employment_type_code as $key => $employment_type_code){
$leavetypedetail = new HrLeaveTypeDetail();
$leavetypedetail->no_of_days = $request->no_of_days[$key];
$leavetypedetail->employment_type_code = $request->employment_type_code[$key];
$leavetypedetail->leave_type_id = $leavetype->id;
$leavetypedetail->save();
}
DB::commit();
Session::flash('success', 'Leave Type is updated successfully');
return redirect()->route('leave.leave_types.index');
}
catch (\Illuminate\Database\QueryException $e){
DB::rollback();
$errorCode = $e->errorInfo[1];
if($errorCode == 1062){
Session::flash('error', 'Duplicate Entry! Please try again');
}
return back();
}
catch (Exception $exception) {
DB::rollback();
Session::flash('error', 'Leave Type update failed!');
return redirect()->route('leave.leave_types.index');
}
}
view blade
<form action="{{route('leave.leave_types.update', ['id'=>$leavetype->id])}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
<div class="card-body">
<div class="form-body">
<div class="row">
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Leave Type Name:<span style="color:red;">*</span></label>
<input type="text" name="leave_type_name" placeholder="Enter leave type name here" class="form-control" value="{{old('leave_type_name',$leavetype->leave_type_name)}}">
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Leave Type Code:</label>
<input type="text" name="leave_type_code" placeholder="Enter leave type code here" class="form-control" value="{{old('leave_type_code',$leavetype->leave_type_code)}}">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label>Description</label>
<textarea rows="2" name="description" class="form-control" placeholder="Enter Description here ..." value="{{old('description',$leavetype->description)}}">{{old('description',$leavetype->description)}}</textarea>
</div>
</div>
<div class="col-sm-12">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Employment Type<span style="color:red;">*</span></th>
<th scope="col">Leave Days<span style="color:red;">*</span></th>
<th scope="col"><a class="btn btn-info addRow"><i class="fa fa-plus"></i></a></th>
</tr>
</thead>
<tbody>
#foreach($leavetypedetails as $leavetypedetail)
<tr>
<td width="60%">
<select class="form-control select2bs4" data-placeholder="Select Employment Type" tabindex="1" name="employment_type_code[]">
<option name="employment_type_code[]" value="{{$leavetypedetail->employmenttype->employment_type_code}}">{{$leavetypedetail->employmenttype->employment_type_name}}</option>
#foreach($employmenttypes as $employmenttype)
<option name="employment_type_code[]" value="{{$employmenttype->employment_type_code}}">{{$employmenttype->employment_type_name}}</option>
#endforeach
</select>
</td>
<td width="35%"><input value="{{$leavetypedetail->no_of_days}}" type="text" name="no_of_days[]" class="form-control" max="120"></td>
<td width="5%"><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">{{ trans('global.update') }}</button>
<button type="button" onclick="window.location.href='{{route('leave.leave_types.index')}}'" class="btn btn-default">Cancel</button>
</div>
</form>
<script type="text/javascript">
$(document).ready(function(){
$('.addRow').on('click', function () {
addRow();
});
function addRow() {
var addRow = '<tr>\n' +
' <td width="60%"><select class="form-control select2bs4" data-placeholder="Choose Employment Type" tabindex="1" name="employment_type_code[]">\n' +
' <option value="0" selected="true" disabled="true">Select Employment Type</option>\n' +
' #if($employmenttypes->count() > 0 )\n' +
' #foreach($employmenttypes as $employmenttype)\n' +
' <option value="{{$employmenttype->employment_type_code}}">{{$employmenttype->employment_type_name}}</option>\n' +
' #endforeach\n' +
' #endif\n' +
' </select></td>\n' +
' <td width="35%"><input type="number" name="no_of_days[]" placeholder="Enter leave days here" class="form-control no_of_days" max="120"></td>\n' +
' <td width="5%"><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>\n' +
' </tr>';
$('tbody').append(addRow);
addRemoveListener();
};
addRemoveListener();
});
When I submitted the form for update,
I observed that the application save another data (duplicated) the model HrLeaveType into the database.
It replace the value of employment_type_code in the HrLeaveTypeDetail model with the latest row in HrLeaveType.
Why am I having these issues and how do I resolve it?
Thanks
I have a dynamic form that successfully adds multiple rows by clicking on the add button. the problem is when I try to save data into the database it throws the below error.
{message: "Illegal string offset 'supplier_id'", exception:
"ErrorException",…} exception: "ErrorException" file:
"C:\xampp\htdocs\Bookstore\app\Http\Controllers\API\PurchaseController.php"
line: 87 message: "Illegal string offset 'supplier_id'" trace: [{file:
"C:\xampp\htdocs\Bookstore\app\Http\Controllers\API\PurchaseController.php",
line: 87,…},…]
and help will be highly appreciated
Code in the controller
public function store(Request $request)
{
$products = json_decode($request->getContent('myArray') , true);
foreach( $products as $product )
{
Purchase::create([
'supplier_id' => $product['supplier_id'],
'date' => $product['date'],
'totalAmount' => $product['totalAmount'],
'description' => $product['description']
]);
}
// return dd($myArray);
return response()->json($Purchase);
}
Form in the Purchases Vue
<form
#submit.prevent="
editMode ? updatePurchase() : createPurchase()
"
>
<div class="modal-body">
<div class="form-horizontal">
<tr v-for="(invoice_product, k) in invoice_products" :key="k">
<td scope="row" class="trashIconContainer">
<i class="fa fa-trash" #click="deleteRow(k, invoice_product)"></i>
</td>
<td style="width: 20%;">
<select
name="supplier_id[]"
id="supplier_id"
:class="{
'is-invalid': form.errors.has(
'supplier_id'
)
}"
class="form-control"
v-model="invoice_product.supplier_id"
data-live-search="true"
>
<option value selected>د پلورونکي ټاکنه</option>
<option
v-for="Supplier in Suppliers"
:key="Supplier.id"
:value="Supplier.id"
>{{ Supplier.name }}</option>
</select>
<has-error :form="form" field="supplier_id"></has-error>
</td>
<td style="width: 20%;padding-right: 10px;">
<input
dir="rtl"
id="text1"
v-model="invoice_product.date"
placeholder="نیټه "
type="date"
name="date[]"
class="form-control"
:class="{
'is-invalid': form.errors.has('date')
}"
/>
<has-error :form="form" field="date"></has-error>
</td>
<td style="width: 20%;padding-right: 10px;">
<input
dir="rtl"
id="text1"
v-model="invoice_product.totalAmount"
placeholder=" ټولی پیسی "
type="number"
name="totalAmount[]"
class="form-control"
:class="{
'is-invalid': form.errors.has(
'totalAmount'
)
}"
/>
<has-error :form="form" field="totalAmount"></has-error>
</td>
<td style="width: 40%;padding-right: 10px;">
<textarea
v-model="invoice_product.description"
placeholder="تشریح"
type="text"
name="description[]"
class="form-control"
:class="{
'is-invalid': form.errors.has(
'description'
)
}"
></textarea>
<has-error :form="form" field="description"></has-error>
</td>
</tr>
<button type="button" class="btn btn-info" #click="addNewRow">
<i class="fa fa-plus-circle"></i>
Add
</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">لغوه کړی</button>
<button
v-show="editMode"
:disabled="form.busy"
type="submit"
class="btn btn-success"
>تازه کړی</button>
<button
v-show="!editMode"
:disabled="form.busy"
type="submit"
class="btn btn-primary"
>خوندی کړی</button>
</div>
</div>
</form>
Sript in the Purchases Vue
data() {
return {
invoice_products: [
{
supplier_id: "",
totalAmount: "",
date: "",
description: ""
}
],
}
deleteRow(index, invoice_product) {
var idx = this.invoice_products.indexOf(invoice_product);
console.log(idx, index);
if (idx > -1) {
this.invoice_products.splice(idx, 1);
}
this.calculateTotal();
},
addNewRow() {
this.invoice_products.push({
supplier_id1: "",
totalAmount1: "",
date1: "",
description1: ""
});
},
createPurchase() {
axios
.post("api/Purchase", {
myArray: this.invoice_products
})
.then(() => {
$("#addNew").modal("hide");
toast.fire({
icon: "success",
html: "<h5> معلومات په بریالیتوب سره خوندي شول</h5>"
});
Fire.$emit("refreshPage");
this.form.reset();
})
.catch(er => {
console.log(er);
});
},
I found the solution
public function store(Request $request)
{
if ($purchases= $request->get('myArray')) {
foreach($purchases as $purchase) {
Purchase::create([
'supplier_id' => $purchase['supplier_id'],
'date' => $purchase['date'],
'totalAmount' => $purchase['totalAmount'],
'description' => $purchase['description']
]);
}
}
return response()->json();
}
I derived some data from jQuery in Laravel-5.8:
Model Class:
class HrLeaveRequest extends Model
{
protected $table = 'hr_leave_requests';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'company_id',
'employee_id',
'reason',
'leave_type_id',
'commencement_date',
'resumption_date',
'authorized_days',
'available_days',
'leave_status',
'no_of_days',
'relief_officer_id',
'contact_person_fullname',
'contact_person_phone',
'contact_person_address',
'created_by',
'created_at',
'updated_by',
'updated_at',
'is_active'
];
protected $dates = [];
protected $casts = [];
public function reliefofficer()
{
return $this->belongsTo('App\Models\Hr\HrEmployee','relief_officer_id');
}
public function leavetype()
{
return $this->belongsTo('App\Models\Hr\HrLeaveType','leave_type_id');
}
}
Controller
public function store(StoreLeaveRequestRequest $request)
{
$leaverequest = HrLeaveRequest::create([
'leave_type_id' => $request->leave_type_id,
'employee_id' => $userEmployee,
'commencement_date' => $commencementDate,
'resumption_date' => $resumptionDate,
'authorized_days' => $request->authorized_days,
'available_days' => $request->available_days,
'no_of_days' => $days,
'reason' => $request->reason,
'relief_officer_id' => $request->relief_officer_id,
'contact_person_fullname' => $request->contact_person_fullname,
'contact_person_phone' => $request->contact_person_phone,
'contact_person_address' => $request->contact_person_address,
'company_id' => Auth::user()->company_id,
'created_by' => Auth::user()->id,
'created_at' => date("Y-m-d H:i:s"),
'is_active' => 1,
]);
Session::flash('success', 'Leave Request is created successfully');
return redirect()->route('service.leave_requests.index');
}
<script type="text/javascript">
$(document).ready(function() {
$(document).on('change', '#leave_type', function() {
var air_id = $(this).val();
var a = $(this).parent();
var op = "";
$.ajax({
type: 'get',
url: '{{ route('get.leavecounts.all') }}',
data: { 'id': air_id },
dataType: 'json', //return data will be json
success: function(data) {
$('#authorized_leave_days').val(data.authorizedleavedays);
$('#available_leave_daysx').val(data.availableleavedays);
let sumAvailableDays = parseInt(data.authorizedleavedays) - parseInt(data.availableleavedays);
$("#available_leave_days").val(sumAvailableDays);
},
error:function(){
}
});
});
});
</script>
I derived 'authorized_days' and 'available_days' from the jQuery above and loaded the result into:
view
<form action="{{route('service.leave_requests.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<div class="col-sm-4">
<div class="form-group">
<label>Leave Type:<span style="color:red;">*</span></label>
<select id="leave_type" class="form-control select2bs4" data-placeholder="Choose Leave Type" tabindex="1" name="leave_type_id" style="width: 100%;">
<option value="">Select Leave Type</option>
#if($leavetypes->count() > 0)
#foreach($leavetypes as $leavetype)
<option value="{{$leavetype->id}}">{{$leavetype->leave_type_name}}</option>
#endforeach
#endif
</select>
</div>
</div>
<input type="hidden" id="available_leave_daysx" class="form-control" value="0" >
<div class="col-sm-4">
<div class="form-group">
<label>Commencement Date:<span style="color:red;">*</span></label>
<input type="date" class="form-control" placeholder="dd/mm/yyyy" name="commencement_date" value="{{old('commencement_date')}}" min="{{Carbon\Carbon::now()->addDay()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Resumption Date:<span style="color:red;">*</span></label>
<input type="date" class="form-control" placeholder="dd/mm/yyyy" name="resumption_date" value="{{old('resumption_date')}}" min="{{Carbon\Carbon::now()->addDay()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Relief Officer:<span style="color:red;">*</span></label>
<select class="form-control select2bs4" data-placeholder="Choose Relief Officer" tabindex="1" name="relief_officer_id" style="width: 100%;">
<option value="">Select Relief Officer</option>
#if($reliefofficers->count() > 0)
#foreach($reliefofficers as $reliefofficer)
<option value="{{$reliefofficer->id}}">{{$reliefofficer->employee_code}} - {{$reliefofficer->first_name}} {{$reliefofficer->last_name}}</option>
#endforeach
#endif
</select>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label>Reason</label>
<textarea rows="2" name="reason" class="form-control" placeholder="Enter Reason here" value="{{old('reason')}}"></textarea>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Contact Person Name:<span style="color:red;">*</span></label>
<input type="text" name="contact_person_fullname" placeholder="Enter contact person name here" class="form-control" value="{{old('contact_person_fullname')}}">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Contact Person Phone:<span style="color:red;">*</span></label>
<input type="number" name="contact_person_phone" placeholder="e.g. 23455996633" class="form-control" value="{{old('contact_person_phone')}}" pattern="[0-9]{13}" style="width: 100%;" maxlength="14">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Authorized Leave Days:</label>
<input type="number" id="authorized_leave_days" class="form-control authorized_days" name="authorized_days" value="{{old('authorized_days')}}" style="width: 100%;" disabled>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Available Leave Days:</label>
<input type="number" id="available_leave_days" class="form-control available_days" name="available_days" value="{{old('available_days')}}" style="width: 100%;" disabled>
</div>
</div>
<div class="card-footer">
<button type="submit" id="submit_create" class="btn btn-primary">{{ trans('global.save') }}</button>
'service.leave_requests.index')}}'" class="btn btn-default">Cancel
<script type="text/javascript">
$(document).ready(function() {
$(document).on('change', '#leave_type', function() {
var air_id = $(this).val();
var a = $(this).parent();
var op = "";
$.ajax({
type: 'get',
url: '{{ route('get.leavecounts.all') }}',
data: { 'id': air_id },
dataType: 'json', //return data will be json
success: function(data) {
$('#authorized_leave_days').val(data.authorizedleavedays);
$('#available_leave_daysx').val(data.availableleavedays);
let sumAvailableDays = parseInt(data.authorizedleavedays) - parseInt(data.availableleavedays);
$("#available_leave_days").val(sumAvailableDays);
},
error:function(){
}
});
});
});
</script>
And I see the data there clearly on my screen, but when I saved it I found null in each of the fields.
Then I did:
dd($leaverequest);
and I have this:
dd($leaverequest):
#attributes: array:17 [▼
"leave_type_id" => "1"
"employee_id" => 2
"commencement_date" => Carbon\Carbon #1586559600 {#804 ▶}
"resumption_date" => Carbon\Carbon #1587769200 {#790 ▶}
"authorized_days" => null
"available_days" => null
"no_of_days" => 11
"relief_officer_id" => "10"
"contact_person_fullname" => "assasasaddddd"
"contact_person_phone" => "08099448844"
"contact_person_address" => "xxzxzxzxz ddds"
"company_id" => 1
"created_by" => 2
"created_at" => "2020-04-10 22:10:41"
"is_active" => 1
"id" => 9
]
You can see:
"authorized_days" => null
"available_days" => null
How do I resolve this?
Thank you
Using Laravel-5.8, I am trying to delevop a web application.I have two tables: goals and goal_details. Goal is the main model class.
Using Rules and Request in Laravel:
public function rules()
{
return [
'goal_title' => 'required|min:5|max:100',
'goal_type_id' => 'required',
'weighted_score' => 'required|numeric|min:0|max:500',
'start_date' => 'required',
'end_date' => 'required|after_or_equal:start_date',
'kpi_description' => 'required',
'activity' => 'required',
];
}
goals: customer_id, goal_title, weighted_score, start_date, end_date
goal_details: goal_type_id, kpi_description
public function create()
{
$userCompany = Auth::user()->company_id;
$identities = DB::table('appraisal_identity')->select('id','appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();
$goaltypes = GoalType::where('company_id', $userCompany)->get();
$categories = GoalType::with('children')->where('company_id', $userCompany)->whereNull('parent_id')->get();
return view('goals.create')
->with('goaltypes', $goaltypes)
->with('categories', $categories)
->with('identities', $identities);
}
public function store(StoreGoalRequest $request)
{
$startDate = Carbon::parse($request->start_date);
$endDate = Carbon::parse($request->end_date);
$userCompany = Auth::user()->company_id;
$employeeId = Auth::user()->employee_id;
$goal = new Goal();
$goal->goal_type_id = $request->goal_type_id;
$goal->appraisal_identity_id = $request->appraisal_identity_id;
$goal->employee_id = $employeeId;
$goal->weighted_score = $request->weighted_score;
$goal->goal_title = $request->goal_title;
$goal->goal_description = $request->goal_description;
$goal->start_date = $startDate;
$goal->end_date = $endDate;
$goal->save();
foreach ( $request->activity as $key => $activity){
$goaldetail = new GoalDetail();
$goaldetail->kpi_description = $request->kpi_description[$key];
$goaldetail->activity = $request->activity[$key];
$goaldetail->appraisal_goal_id = $goal->id;
$goaldetail->save();
}
Session::flash('success', 'Goal is created successfully');
return redirect()->route('goals.index');
}
create.blade.php
<div class="row">
<div class="col-md-12">
<!-- general form elements -->
<div class="card card-secondary">
<!-- /.card-header -->
<!-- form start -->
<form method="POST" action="{{route('goals.store')}}">
#csrf
<div class="card-body">
<div class="form-body">
<div class="row">
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Goal Type:<span style="color:red;">*</span></label>
<select id="goal_type" class="form-control" name="goal_type_id">
<option value="">Select Goal Type</option>
#foreach ($categories as $category)
#unless($category->name === 'Job Fundamentals')
<option disabled="disabled" value="{{ $category->id }}" {{ $category->id == old('category_id') ? 'selected' : '' }}>{{ $category->name }}</option>
#if ($category->children)
#foreach ($category->children as $child)
#unless($child->name === 'Job Fundamentals')
<option value="{{ $child->id }}" {{ $child->id == old('category_id') ? 'selected' : '' }}> {{ $child->name }}</option>
#endunless
#endforeach
#endif
#endunless
#endforeach
</select>
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Goal Title:<span style="color:red;">*</span></label>
<input type="text" name="goal_title" placeholder="Enter goal title here" class="form-control">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label>Goal Description</label>
<textarea rows="2" name="goal_description" class="form-control" placeholder="Enter Goal Description here ..."></textarea>
</div>
</div>
<div class="col-sm-12">
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Activity<span style="color:red;">*</span></th>
<th scope="col">KPI Description<span style="color:red;">*</span></th>
<th scope="col">Attachment</th>
<th scope="col"><a class="addRow"><i class="fa fa-plus"></i></a></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="activity[]" class="form-control activity" ></td>
<td><input type="text" name="kpi_description[]" class="form-control kpi" ></td>
<td>
<div class="custom-file">
<input type="file" name="appraisal_doc[]" class="custom-file-input" id="customFile">
<label class="custom-file-label" for="exampleInputFile">Choose file</label>
</div>
</td>
<td><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>
</tr>
</tbody>
</table>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
<label class="control-label"> Weight:</label>
<input type="number" name="weighted_score" placeholder="Enter weighted score here" class="form-control">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
<label class="control-label"> Start Date:<span style="color:red;">*</span></label>
<input type="date" class="form-control" placeholder="dd/mm/yyyy" name="start_date" min="{{Carbon\Carbon::now()->format('Y-m-d')}}">
</div>
</div>
<div class="col-12 col-sm-4">
<div class="form-group">
<label class="control-label"> End Date:<span style="color:red;">*</span></label>
<input type="date" class="form-control" placeholder="dd/mm/yyyy" name="end_date" min="{{Carbon\Carbon::now()->format('Y-m-d')}}">
</div>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
<button type="button" onclick="window.location.href='{{route('appraisal.appraisal_goals.index')}}'" class="btn btn-default">Cancel</button>
</div>
</form>
</div>
<!-- /.card -->
</div>
<!--/.col (left) -->
</div>
javascript
<script type="text/javascript">
$(document).ready(function(){
$('.addRow').on('click', function () {
var isHod = {{ Auth::user()->is_hod == 0 ? 0 : 1 }};
var numRows = $('.activity').length
if (isHod || (!isHod && numRows<3)) {
addRow();
}
});
function addRow() {
var addRow = '<tr>\n' +
' <td><input type="text" name="activity[]" class="form-control activity" ></td>\n' +
' <td><input type="text" name="kpi_description[]" class="form-control kpi_description" ></td>\n' +
' <td><div class="custom-file"><input type="file" name="appraisal_doc[]" class="custom-file-input" id="customFile"><label class="custom-file-label" for="exampleInputFile">Choose file</label></div></td>\n' +
' <td><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>\n' +
' </tr>';
$('tbody').append(addRow);
addRemoveListener();
};
addRemoveListener();
});
function addRemoveListener() {
$('.remove').on('click', function () {
var l =$('tbody tr').length;
if(l==1){
alert('you cant delete last one')
}else{
$(this).parent().parent().remove();
}
});
}
</script>
When I submitted, I observed that the fields from goal_details: goal_type_id, kpi_description are not validated as required. It allows null. But all fields in goals are validated.
How do I resolve this?
Thank you.
Check if it can solve your "kpi_description" validation problem :
public function rules()
{
return [
'goal_title' => 'required|min:5|max:100',
'goal_type_id' => 'required',
'weighted_score' => 'required|numeric|min:0|max:500',
'start_date' => 'required',
'end_date' => 'required|after_or_equal:start_date',
'kpi_description' => 'required|array',
'kpi_description.*' => 'required',
'activity' => 'required',
];
}
after defining rules you have to call "validated" method as needed.
try this :
public function store(StoreGoalRequest $request)
{
$validated = $request->validated();
$startDate = Carbon::parse($request->start_date);
$endDate = Carbon::parse($request->end_date);
$userCompany = Auth::user()->company_id;
$employeeId = Auth::user()->employee_id;
$goal = new Goal();
$goal->goal_type_id = $request->goal_type_id;
$goal->appraisal_identity_id = $request->appraisal_identity_id;
$goal->employee_id = $employeeId;
$goal->weighted_score = $request->weighted_score;
$goal->goal_title = $request->goal_title;
$goal->goal_description = $request->goal_description;
$goal->start_date = $startDate;
$goal->end_date = $endDate;
$goal->save();
foreach ( $request->activity as $key => $activity){
$goaldetail = new GoalDetail();
$goaldetail->kpi_description = $request->kpi_description[$key];
$goaldetail->activity = $request->activity[$key];
$goaldetail->appraisal_goal_id = $goal->id;
$goaldetail->save();
}
Session::flash('success', 'Goal is created successfully');
return redirect()->route('goals.index');
}