Checkout Form isn't Submitting - laravel

I'm trying to submit the checkout form, but it is not submitting. It has values of products form cart (product name, code, price) and currently entered user data, and screenshot of DB table (orders) https://ibb.co/bbBHCky ...... any solution to resolve it,..........,,,,,,,,.
checkout.blade.php
<ul class="list-group mb-3">
<?php $total_amount = 0; ?>
#foreach($userCart as $cart)
<li class="list-group-item d-flex justify-content-between lh-condensed">
<div>
<h6 class="my-0">{{ $cart->product_name }}</h6>
<small class="text-muted">Quantity: {{ $cart->quantity }}</small>
<small class="text-muted" style="display: none;">{{ $cart->product_code }}</small>
<small class="text-muted" style="display: none;">{{ $cart->product_id }}</small>
</div>
<span class="text-muted">Rs {{ $cart->price }}</span>
</li>
#endforeach
<li class="list-group-item d-flex justify-content-between">
<span>Total (PKR)</span>
<strong>{{ $total }}</strong>
</li>
</ul>
<form method="post" action="{{ url('/checkout') }}" class="needs-validation" novalidate>{{ csrf_field() }}
<div class="row">
<div class="col-md-6 mb-3">
<label for="firstName">Name</label>
<input type="text" name="name" class="form-control" id="firstName" placeholder="name" value="" required
style="border: 1px solid black !important;">
<div class="invalid-feedback">
Valid first name is required.
</div>
</div>
</div>
<div class="mb-3">
<label for="email">Email </label>
<input type="email" name="email" class="form-control" id="email" placeholder="you#example.com"
required="required" style="border: 1px solid black !important;">
<div class="invalid-feedback">
Please enter a valid email address for shipping updates.
</div>
</div>
<div class="mb-3">
<label for="address">Address</label>
<input type="text" name="address" class="form-control" id="address" placeholder="1234 Main St" required
style="border: 1px solid black !important;">
<div class="invalid-feedback">
Please enter your shipping address.
</div>
</div>
<div class="row">
<div class="col-md-5 mb-3">
<label for="country">Country</label>
<select name="country" class="custom-select d-block w-100" id="country" required>
<option value="">Choose...</option>
<option value="usa">United States</option>
</select>
<div class="invalid-feedback">
Please select a valid country.
</div>
</div>
<div class="col-md-4 mb-3">
<label for="state">State</label>
<select name="state" class="custom-select d-block w-100" id="state" required>
<option value="">Choose...</option>
<option value="cal">California</option>
</select>
<div class="invalid-feedback">
Please provide a valid state.
</div>
</div>
<div class="col-md-5 mb-3">
<label for="country">City</label>
<select name="city" class="custom-select d-block w-100" id="country" required>
<option value="">Choose...</option>
<option value="lhr">United States</option>
</select>
<div class="invalid-feedback">
Please select a valid country.
</div>
</div>
<div class="col-md-3 mb-3">
<label for="zip">Zip</label>
<input type="text" name="zipcode" class="form-control" id="zip" placeholder="" required
style="border: 1px solid black !important;">
<div class="invalid-feedback">
Zip code required.
</div>
</div>
<div class="col-md-5 mb-3">
<label for="zip">Mobile</label>
<input type="text" name="mobile" class="form-control" id="zip" placeholder="" required
style="border: 1px solid black !important;">
<div class="invalid-feedback">
mobile no required.
</div>
</div>
</div>
<button class="btn btn-primary btn-lg btn-block" type="submit" style="background-color: black;">Place Order</button>
</form>
ProductsController
public function placeOrder(Request $request)
{
if ($request->isMethod('post')) {
$data = $request->all();
$session_id = Session::get('session_id');
if (empty($session_id)) {
$session_id = str_random(40);
Session::put('session_id', $session_id);
}
$order = new Order;
$order->id = $id;
$order->name = $name;
$order->email = $email;
$order->address = $address;
$order->country = $country;
$order->state = $state;
$order->city = $city;
$order->zipcode = $zipcode;
$order->mobile = $mobile;
$order->product_id = $product_id;
$order->product_code = $product_code;
$order->product_name = $product_name;
$order->product_price = $product_price;
$order->product_quantity = $qty;
$order->order_status = "New";
$order->grand_total = $data['grand_total'];
$order->save();
return view('products.checkout');
}
}
Route
Route::match(['get','post'],'/place-order','ProductsController#placeOrder');

change form action to
<form method="post" action="{{ url('place-order') }}" class="needs-validation" novalidate>
and remove space from route: '/place- order'
Route::match(['get','post'],'/place-order','ProductsController#placeOrder');

Related

how to validate Unique database field on Controller in laravel

Good Day, I am new in Laravel environment. I am developing a simple school enrollment registration website but having trouble in validation on my controller..
My plan is, when the user will register in the website, username textbox should be validated if the username is already used by other student. i tried every possible tutorial i found in the net but i have no luck...
Here is my page, red circle should be validated if the input username already exist in the database..
Whole code in .blade
<!--Registration start here -->
<form method="Post" action="{{url('store_account_Registration')}}">
#csrf
#if(Session::get('success'))
<div class="alert alert-danger">
{{ Session::get('success')}}
</div>
#endif
#if(Session::get('fail'))
<div class="alert alert-danger">
{{ Session::get('fail')}}
</div>
#endif
<div class="col-sm-12 form-group">
<input type="hidden" class="form-control" name="uniqID3" id="uid" placeholder="Enrollment Registration Number" value="{{ $regid3 }}" >
</div>
<div class="col-sm-12 form-group">
<input type="hidden" class="form-control" name="pfulname3" id="name-f" placeholder="Enrollment Registration Number" required value="{{ $fulname4 }}" >
</div>
<div class="container-fluid" style="margin-bottom: 2em">
<div class="row justify-content-center align-items-center" style="padding: 10px">
<div class="card col-md-5" style="transform: none;>
<div class="card-body">
<div class="alert alert-primary" role="alert" style="margin-top: 1em">
<p>Name: <strong>{{ $fulname4 }}</strong></p>
<p>Enrollment No.: <strong>{{ $regid3 }}</strong></p>
</div>
<input type="hidden" class="hide" id="csrf_token" name="csrf_token" value="C8nPqbqTxzcML7Hw0jLRu41ry5b9a10a0e2bc2">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Username</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-user" aria-hidden="true"></i></span>
</div>
<input type="text" class="form-control" name="username" id="username" placeholder="Username" required value="{{ old('username')}}" >
</div>
<div class="help-block with-errors text-danger">
<span style="color:red">#error('username'){{ $message}}#enderror</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Password</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-lock" aria-hidden="true"></i></span>
</div>
<input type="password" id="password" name="password" pattern="^\S{6,}$" onchange="this.setCustomValidity(this.validity.patternMismatch ? 'Must have at least 6 characters' : ''); if(this.checkValidity()) form.password_two.pattern = this.value;" class="form-control" title="Password is needed" required placeholder="Password" >
</div>
<div class="help-block with-errors text-danger">
<span style="color:red">#error('password'){{ $message}}#enderror</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Confirm Password</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-lock" aria-hidden="true"></i></span>
</div>
<input id="password_two" type="password" name="password_two" pattern="^\S{6,}$" onchange="this.setCustomValidity(this.validity.patternMismatch ? 'Please enter the same Password as above' : '');" placeholder="Confirm Password" class="form-control" title="Confirm Password is needed" required placeholder="Confirm Password" >
</div>
<div class="help-block with-errors text-danger">
<span style="color:red">#error('password_two'){{ $message}}#enderror</span>
</div>
</div>
</div>
</div>
<div class="row" style="margin-top: 1em" >
<div class="col-md-12" style="margin-bottom: 2em">
<input type="hidden" name="redirect" value="">
<!-- <button type="submit" class="btn btn-primary btn-lg btn-block" name="submit">Save OASIS Account</button> -->
<!-- < <p class="btn btn-primary btn-lg btn-block">Save OASIS Account</p> -->
<button type="submit" class="btn btn-primary btn-lg btn-block">Save OASIS Account</button>
</div>
</div>
</div>
</div>
</div>
</div>
<!--Registration start here -->
</div>
</form>
enter code here
I always got this error..
Here is my code..
my validation in my controller
public function store_account_Registration(Request $request)
{
request()->validate([
'username' => 'required|min:6|unique:TempAccount,AccountName,'
]);
// $this->validator($request->all())->validate();
$current_date = date('Y-m-d H:i:s');
$query = DB::table('TempAccount')->insert([
'RegID'=>$request->input('uniqID3'),
'FullName'=>$request->input('pfulname3'),
'AccountName'=>$request->input('username'),
'Pass'=>$request->input('password'),
'created_at'=> $current_date,
'updated_at'=> $current_date,
]);
if($query){
return back()->with('success', 'Data has been Successfyll inserted');
// return view('pages.enrollment_success');
// return redirect()->route('pages.enrollment_GradeLevelSchoolInfo', ['uniqIDd' => 1]);
// return redirect('enrollment_GradeLevelSchoolInfo');
}
else
{
return back()->with('fail', 'something went wrong');
}
}
my Route in web.php
Route::get('pages',[accountregistration::class, 'index']);
Route::Post('store_account_Registration', [accountregistration::class, 'store_account_Registration']);
my action in form..
The issue is in your form. You are not reaching the correct route at all.
Also you are missing the #csrf token or at least not shown in your print screen
<form action="{{url('store_account_Registration')}}" method="POST">
#csrf
In your validation you can remove the trailing coma after AccountName as well.

Use row data to be inserted into table and update current table Laravel 5.7

I use record data from a table to reduce the entries that the user will make as it will be stored in another table, but after saving it must update the status of the current record to be 'Used'
the current row is in table called airfiles and will be inserted after adding sale amount into table called tickets
here is the airfiles schema code :
public function up()
{
Schema::create('airfiles', function (Blueprint $table) {
$table->increments('id');
$table->string('airl')->nullable();
$table->string('num')->nullable();
$table->date('date')->nullable();
$table->string('pass')->nullable();
$table->string('route')->nullable();
$table->string('dot')->nullable();
$table->string('dor')->nullable();
$table->string('flynumt')->nullable();
$table->string('flynumr')->nullable();
$table->float('fare')->nullable();
$table->float('tax')->nullable();
$table->float('total')->nullable();
$table->float('comm')->nullable();
$table->float('net')->nullable();
$table->string('rloc')->nullable();
$table->string('son')->nullable();
$table->string('acc')->nullable();
$table->string('pcc')->nullable();
$table->tinyInteger('progid')->default('1');
$table->string('status')->nullable();
$table->integer('segment')->nullable();
$table->string('filename')->nullable();
$table->float('k7')->nullable();
$table->string('used')->nullable();
$table->string('transfer')->nullable();
$table->integer('ticket_id')->unsigned()->nullable();
$table->rememberToken();
$table->timestamps();
});
Schema::table('airfiles', function (Blueprint $table) {
$table->foreign('ticket_id')->references('id')->on('tickets')->onDelete('cascade');
});
}
and here is the TicketController code:
public function store(Request $request, $id)
{
$this->validate($request,[
'date'=>'required',
'tkt_no'=>'required',
'sector'=>'required',
'airline'=>'required',
'supplier'=>'required',
'fare'=>'required',
'tax'=>'required',
'total_tax'=>'required',
'total_cost'=>'required',
'k7'=>'required',
'comm'=>'required',
'profit'=>'required',
'sale_price'=>'required',
'sign'=>'required',
'pax_name'=>'required',
'mob'=>'required',
'amount_arabic'=>'required',
'amount_english'=>'required',
'action_type'=>'required',
'trans_type'=>'required',
'payment_type'=>'required',
]);
$ticket = new Ticket();
$ticket->date = $request->get('date');
$ticket->tkt_no = $request->get('tkt_no');
$ticket->sector = $request->get('sector');
$ticket->airline = $request->get('airline');
$ticket->supplier = $request->get('supplier');
$ticket->fare = $request->get('fare');
$ticket->tax = $request->get('tax');
$ticket->total_tax = $request->get('total_tax');
$ticket->total_cost = $request->get('total_cost');
$ticket->k7 = $request->get('k7');
$ticket->comm = $request->get('comm');
$ticket->profit = $request->get('profit');
$ticket->sale_price = $request->get('sale_price');
$ticket->sign = $request->get('sign');
$ticket->branch =Auth::user()->branch_id;
$ticket->pax_name = $request->get('pax_name');
$ticket->mob = $request->get('mob');
$ticket->amount_arabic = $request->get('amount_arabic');
$ticket->amount_english = $request->get('amount_english');
$ticket->action_type = $request->get('action_type');
$ticket->trans_type = $request->get('trans_type');
$ticket->payment_type = $request->get('payment_type');
$ticket->post_flag = '0';
$ticket->save();
Airfile::find($id)->update(['used' => 'Used']);
session()->flash('success',__('site.added_successfully'));
return redirect()->route('$tickets.index');
}
here is the close blade code :
<div class="card-content collpase show">
<div class="card-body">
<form action="{{ route('tickets.store') }}" method="post">
{{ csrf_field() }}
{{ method_field('post') }}
<div class="row">
<div class="form-group col-md-2">
<label>#lang('site.son')</label>
#if(Auth::user()->shortsign == '--')
<select class="select2-size-lg form-control border-primary"
style="width: 100%;" name="sign">
#foreach ($users as $user)
<option
value="{{ $user->shortsign }}">{{ $user->shortsign }}</option>
#endforeach
</select>
#else
<select class="select2-size-lg form-control border-primary"
style="width: 100%;" name="sign" readonly>
<option
value="{{ Auth::user()->shortsign }}">{{ Auth::user()->shortsign }}</option>
</select>
#endif
</div>
<div class="col-md-3">
<div class="form-group">
<label for="pax_name">#lang('site.pax_name')</label>
<div class="position-relative has-icon-left">
<input type="text" id="pax_name" class="form-control"
name="pax_name"
value="{{ $airfiles->pass }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="client">#lang('site.client')</label>
<div class="position-relative has-icon-left">
<input type="text" id="client" class="form-control"
name="client"
value="{{ $airfiles->pass }}">
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="mob">#lang('site.mob')</label>
<div class="position-relative has-icon-left">
<input type="text" id="mob" class="form-control"
name="mob"
value="{{ old('mob') }}">
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="form-group col-md-2">
<label>#lang('site.supplier')</label>
<select class="form-control select2 status-type" style="width: 100%;"
name="supplier"
id="supplier">
<option selected value="bsp">BSP</option>
<option value="online">Online</option>
</select>
</div>
<div class="form-group col-md-2">
<label>#lang('site.action_type')</label>
<select class="form-control select2 status-type" style="width: 100%;"
name="action_type"
id="action_type">
<option selected value="issue">#lang('site.issue')</option>
<option value="reissue">#lang('site.reissue')</option>
<option value="emd">#lang('site.emd')</option>
<option value="refund">#lang('site.refund')</option>
{{-- <option value="void_charge">#lang('site.void_charge')</option>--}}
</select>
</div>
<div class="form-group col-md-2">
<label>#lang('site.trans_type')</label>
<select class="form-control select2 status-type" style="width: 100%;"
name="trans_type"
id="trans_type">
<option selected value="domestic">#lang('site.domestic')</option>
<option value="international">#lang('site.international')</option>
</select>
</div>
<div class="form-group col-md-2">
<label>#lang('site.payment_type')</label>
<select class="form-control select2 status-type" style="width: 100%;"
name="payment_type"
id="payment_type">
<option selected value="cash">#lang('site.cash')</option>
<option value="credit">#lang('site.credit')</option>
<option value="advance">#lang('site.advance')</option>
<option value="pending">#lang('site.pending')</option>
</select>
</div>
<div class="form-group col-md-2">
<label>#lang('site.airline')</label>
<select class="form-control select2 status-type" style="width: 100%;"
name="airline"
id="airline">
#foreach ($airlines as $airline)
<option value="{{ $airline->code }}">{{ $airline->code }}</option>
#endforeach
</select>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="sale_price">#lang('site.sale_price')</label>
<div class="position-relative has-icon-left">
<input type="text" id="sale_price" class="form-control"
name="sale_price" placeholder="0"
onkeypress="return isNumberKey(event)"
{{-- onblur="findTotal()"--}}
onkeyup="main ();eng_main();findTotal()"
value=" {{ old('sale_price') }}">
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="tkt_no">#lang('site.tkt_no')</label>
<div class="position-relative has-icon-left">
<input type="text" id="tkt_no" class="form-control"
name="tkt_no" onkeypress="return isNumberKey(event)"
value="{{ $airfiles->num }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="rloc">#lang('site.rloc')</label>
<div class="position-relative has-icon-left">
<input type="text" id="rloc" class="form-control"
name="rloc"
value="{{ $airfiles->rloc }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="sector">#lang('site.sector')</label>
<div class="position-relative has-icon-left">
<input type="text" id="sector" class="form-control"
name="sector" placeholder="#lang('site.like') : HAS/JED JED/MAA"
value="{{ $airfiles->route }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="fare">#lang('site.fare')</label>
<div class="position-relative has-icon-left">
<input type="text" id="fare" class="form-control "
onblur="findTotal()"
name="fare" placeholder="0"
{{-- onkeyup="main ()"--}}
value="{{ $airfiles->fare }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="tax">#lang('site.tax')</label>
<div class="position-relative has-icon-left">
<input type="text" id="tax" class="form-control"
name="tax" placeholder="0"
onkeypress="return isNumberKey(event)"
onblur="findTotal()"
{{-- onkeyup="return sum(event)"--}}
value="{{ $airfiles->tax }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="k7">#lang('site.k7')</label>
<div class="position-relative has-icon-left">
<input type="text" id="k7" class="form-control"
name="k7" placeholder="0"
onkeypress="return isNumberKey(event)"
onblur="findTotal()"
{{-- onkeyup="return sum(event)"--}}
value="{{ $airfiles->k7 }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="comm">#lang('site.comm')</label>
<div class="position-relative has-icon-left">
<input type="text" id="comm" class="form-control"
name="comm" placeholder="0"
onkeypress="return isNumberKey(event)"
onblur="findTotal()"
{{-- onkeyup="return sum(event)"--}}
value="{{ $airfiles->comm }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="profit">#lang('site.profit')</label>
<div class="position-relative has-icon-left">
<input type="number" id="profit" class="form-control"
name="profit" placeholder="0"
onkeypress="return isNumberKey(event)"
onblur="findTotal()"
{{-- onkeyup="return sum(event)"--}}
value="{{ number_format(old('profit'), 2) }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="total_tax">#lang('site.total_tax')</label>
<div class="position-relative has-icon-left">
<input type="text" id="total_tax" class="form-control"
name="total_tax" placeholder="0"
onkeypress="return isNumberKey(event)"
onkeyup="return sum(event)"
value="{{ old('total_tax') }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="total_cost">#lang('site.total_cost')</label>
<div class="position-relative has-icon-left">
<input type="text" id="total_cost" class="form-control"
name="total_cost" placeholder="0"
onkeypress="return isNumberKey(event)"
onkeyup="return sum(event)"
value="{{ old('total_cost') }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<label for="date">#lang('site.date')</label>
<div class="input-group">
<input id="date" name="date" class="form-control datepicker" type="date"
value="{{ $airfiles->date }}" readonly>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="amount_arabic">#lang('site.amount_arabic')</label>
<div class="position-relative has-icon-left">
<input type="text" id="amount_arabic" class="form-control"
name="amount_arabic"
onkeypress="return isNumberKey(event)"
onkeyup="return sum(event)"
value=" {{ old('amount_arabic') }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="amount_english">#lang('site.amount_english')</label>
<div class="position-relative has-icon-left">
<input type="text" id="amount_english" class="form-control"
name="amount_english"
onkeypress="return isNumberKey(event)"
onkeyup="return sum(event)"
value=" {{ old('amount_english') }}" readonly>
<div class="form-control-position">
<i class="la la-keyboard-o"></i>
</div>
</div>
</div>
</div>
</div>
<div class="form-actions right">
<button id="slide-toast" type="submit"
class="btn btn-outline-warning btn-min-width box-shadow-2 mr-1 mb-1"><i
class="la la-check-square-o"></i> #lang('site.close')</button>
</div>
</form><!-- end of form -->
</div><!-- end of box -->
</div>
here is the route code :
//airfile routes
Route::resource('airfiles', 'AirfileController')->except(['show']);
//ticket routes
Route::resource('tickets', 'TicketController')->except(['show']);
the error i got is :
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR)
Too few arguments to function App\Http\Controllers\TicketController::store(), 1 passed and exactly 2 expected
any one can help ?
This is because this methods expects an ID and may be you did not pass id from route to this method. you can pass id to route like this:
{{ route('routeName', ['id' => 1]) }}
And make your route like this:
Route::get('appName/{id}', ControllerName#store)->name('routeName');

How to solve Missing required parameters for [Route: update_pns] [URI: update/{id}]

I want to create Update fucntion . i create the form , i displaying dataon this form , i create button submit to submit this data . but after i submit i get error
Missing required parameters for [Route: update_pns] [URI: update/{id}].
its my Controller to showing data
public function edit_pns($id)
{
$pns = Data_pns::findOrFail($id);
if (!$pns)
abort(404);
$agama = DB::table('master_agama')->pluck('agama','id');
$golongan = DB::table('master_golongan')->pluck('golongan','id');
$jabatan = DB::table('master_jabatan')->pluck('nama_jabatan','id');
$unit_kerja = DB::table('master_unit_kerja')->pluck('unit_kerja','id');
$status_pegawai = DB::table('master_status_pegawai')->whereIn('id',[3,4])->pluck('status_pegawai','id');
//dd($pns);
return view('admin/edit/edit_pns', ['pns' => $pns ,'golongan'=>$golongan,
'jabatan'=>$jabatan,'unit_kerja'=>$unit_kerja,'agama'=>$agama,'status_pegawai'=>$status_pegawai]);
}
and its my controller to store data to update
public function update_pns(Request $request,$id)
{
$pns = User::find($id);
$pns->nama = $request->input('nama');
$pns->email = $request->input('email');
$pns->alamat = $request->input('alamat');
$pns->NIK = $request->input('NIK');
$pns->tempat_lahir = $request->input('tempat_lahir');
$pns->tanggal_lahir = $request->input('tanggal_lahir');
$pns->pendidikan = $request->input('pendidikan');
$pns->nomor = $request->input('nomor');
$pns->agama_id = $request->input('agama_id') ;
$pns->status_pegawai_id = $request->input('status_pegawai_id');
$pns->unit_kerja_id = $request->input('unit_kerja_id') ;
dd($pns);
// $pns->update();
// return redirect('pns1')->with('success', 'Your info are updated');
}
and its this route
Route::get('/edit_pns/{id}', 'AdminController#edit_pns' );
Route::post('/update/{id}', 'AdminController#update_pns')->name('update_pns');
whats wrong about this ?
UPDATE
<form method="POST" action="{{ route('update_pns') }}" class="form-horizontal form-label-left">
#csrf
<span class="section">Lengkapi Form Dibawah ini :</span>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="nama">NAMA <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="nama" type="text" class="form-control #error('nama') is-invalid #enderror" name="nama" value="{{$pns->users->nama}}" required autocomplete="nama">
#error('nama')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="NIK">NIK <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="NIK" type="text" class="form-control #error('NIK') is-invalid #enderror" name="NIK" value="{{$pns->users->NIK}}" required autocomplete="NIK" placeholder="3314********">
#error('NIK')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="email">Email <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{$pns->users->email}}" required autocomplete="email" placeholder="contoh#gmail.com" required>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="tempat_lahir">Tempat Lahir <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="tempat_lahir" type="text" class="form-control #error('tempat_lahir') is-invalid #enderror" name="tempat_lahir" value="{{$pns->users->tempat_lahir}}" required autocomplete="tempat_lahir">
#error('tempat_lahir')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="tanggal_lahir">Tanggal Lahir <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="tanggal_lahir" type="date" class="form-control #error('tanggal_lahir') is-invalid #enderror" name="tanggal_lahir" value="{{$pns->users->getOriginal('tanggal_lahir')}}" required autocomplete="tanggal_lahir">
#error('tanggal_lahir')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label for="jenis_kelamin" class="control-label col-md-3">Jenis Kelamin <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<td>
<label> Laki-Laki<input type="radio" name="jenis_kelamin" value="L" checked>
</label>
</td>
<td>
<label> Perempuan
<input type="radio" name="jenis_kelamin" value="P">
</label>
</td>
#error('jenis_kelamin')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="pendidikan">Pendidikan Terakhir <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="pendidikan" type="text" class="form-control #error('pendidikan') is-invalid #enderror" name="pendidikan" value="{{$pns->users->pendidikan}}" required autocomplete="pendidikan">
#error('pendidikan')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="nomor">Nomor HP <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="nomor" type="text" class="form-control #error('nomor') is-invalid #enderror" name="nomor" value="{{$pns->users->nomor}}" required autocomplete="nomor" placeholder="contoh 628572510294">
#error('nomor')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Agama <span class="required">*</span></label>
<div class="col-md-6 col-sm-9 col-xs-12">
<select name="agama_id" id="agama_id" class="form-control">
#foreach($agama as $masters => $agamas )
<option value="{{ $masters }}" #if ($pns->users->agama_id == $masters ) selected #endif>{{ $agamas }}</option>
#endforeach
</select>
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="alamat">Alamat <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<textarea id="alamat" required="required" name="alamat" value="" class="form-control col-md-7 col-xs-12">{{$pns->users->alamat}}</textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Unit Kerja <span class="required">*</span></label>
<div class="col-md-6 col-sm-9 col-xs-12">
<select name="unit_kerja_id" id="unit_kerja_id" class="form-control">
#foreach($unit_kerja as $units => $unit )
<option value="{{ $units }}" #if ($pns->users->unit_kerja_id == $units ) selected #endif>{{ $unit }}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Status Pegawai <span class="required">*</span></label>
<div class="col-md-6 col-sm-9 col-xs-12">
<select name="status_pegawai_id" id="status_pegawai_id" class="form-control">
#foreach($status_pegawai as $status => $pegawai )
<option value="{{ $status }}" #if ($pns->users->status_pegawai_id == $status ) selected #endif>{{ $pegawai }}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<button id="send" type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
Your route require a parameter in order to be resolved, {id}, so instead of route('update_pns') inside the action of your form, you should have used route('update_pns', $idDesired) and instead of $idDesired, you should write the id you want to get on the update_pns method as $id
Since you're using request in your controller
Change your route to:
Route::post('/update', 'AdminController#update_pns')->name('update_pns');
And to your controller you can do this.
public function update_pns(Request $request)
{
$pns = User::find($request->id);
...
}
Also create a hidden input to your html to get the request id
<input tpye="hidden" value="{{ $id }}" name="id" />

I keep getting an Integrity constraint violation when I'm trying to submit data to db

I have this problem and I have no idea why it's not working. I have tried so many code changes but I still get the same error when I try to submit a form. It's a simple form to insert data to tomany tables in db. All tables accept data with no error but payment_agrement table gives me this error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (kassemschool.payment_agrements, CONSTRAINT payment_agrements_responisbleid_foreign FOREIGN KEY (responsible_id) REFERENCES payment_agrements (id)) (SQL: insert into payment_agrements (frais_inscription, payment_mois, Assurance, Garde, Transport, responsible_id, updated_at, created_at) values (450, 550, 400, 200, 300, 154, 2019-11-16 19:47:39, 2019-11-16 19:47:39))
This is my controller:
if(request()->has('rName')) {
$responsible = Responsible::create([
'ResponsibleName'=> $request->rName,
'ResponsibleCNI'=>$request->cni,
'responsible_number'=>$request->tele
]);
}
//Student::create($data);
if(request()->has('MontantP')) {
$payment=Payment::create([
'PaymentPaid'=>$request->MontantP,
'PaymentRemaining'=>$request->RestP,
'responsible_id'=>$responsible->id,
]);
}
if($request->hasFile('file')){
$student->images = $request->file->store('file');
}
if(request()->has('TransportR')) {
$transport=Transport::create([
'TransportPaid'=> $request->TransportP,
'TransportRemainning'=>$request->TransportR,
'TransportDateStart'=>$request->TdateStart,
'TransportDateEnd'=>$request->TdateEnd,
]);
}
if(request()->has('edateStart')) {
$payment_date = Payment_Date::create([
'PaymentDateStart'=>$request->edateStart,
'PaymentDateEnd'=>$request->edateEnd,
'payment_id'=>$payment->id,
]);
$letter = 'KassN';
$number =$request->env . 'G';
$number2 = $request->group;
$id='_'.rand();
$random = $letter.$number.$number2.$id;
if(request()->has('fName')) {
$student = Student::create([
'StudentFName'=> $request->fName,
'StudentLName'=> $request->lName,
'StudentDateStart'=>$request->edateStart,
'StudentDateEnd'=>$request->edateEnd,
'level_id'=> $request->env,
'group_id'=>$request->group,
'responsible_id'=> $responsible->id,
'payment_date_id'=>$payment_date->id,
'Payment_dure_id'=>$request->paymentD,
'payment_id'=>$payment->id,
'transport_id'=>$transport->id,
'student_code'=>$random
]);
}
if(request()->has('inscription')){
$agrement= Payment_agrement::create([
'frais_inscription'=>$request->inscription,
'payment_mois'=>$request->payment,
'Assurance'=>$request->assaurance,
'Garde'=>$request->garde,
'Transport'=>$request->fraiTransport,
'responsible_id'=>$responsible->id
]);
}
my create view
#extends('layouts.app')
#section('content')
#if (Route::has('login'))
#auth
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
back to <strong>Home</strong>
back to <strong>list</strong>
</div>
#endif
<div class="mr-auto offset-md-1" style="width: 200px;">
<input type="button" class="btn btn-danger btn-sm" name="" value="<- Reteur">
</div>
<div class="card-body">
<div class="container" style="width: 80%;">
<form action="{{url('student')}}" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="firstName">First Name </label>
<input type="text" class="form-control" name="fName" id="elastName">
</div>
<div class="form-group">
<label for="lastName">Last name:</label>
<input type="text" class="form-control" name="lName" id="efirstName">
</div>
<div class="form-group">
<label for="responsable">Responsable Name:</label>
<input type="text" class="form-control" name="rName" id="responsable">
</div>
<div class="form-group">
<label for="cni">Responsable CNI:</label>
<input type="text" class="form-control" name="cni" id="cni">
</div>
<div class="form-group">
<label for="nuveau">Niveau:</label>
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="nuveau">Options</label>
</div>
<select class="custom-select" id="nuveau" name="env">
<option selected>Choose...</option>
#foreach($levels as $level)
<option value="{{$level->id}}">{{$level->LevelName}} </option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<fieldset>
<legend>Aggrement:<hr></legend>
<div class="form-group">
<label for="cni">Frais de inscription:</label>
<input type="text" class="form-control" name="inscription" id="cni">
</div>
<div class="form-group">
<label for="cni">paiement mensuel:</label>
<input type="text" class="form-control" name="payment" id="cni">
</div>
<div class="form-group">
<label for="cni">Frais de Assurance:</label>
<input type="text" class="form-control" name="assaurance" id="cni">
</div>
<div class="form-group">
<label for="cni">Frais de Garde:</label>
<input type="text" class="form-control" name="garde" id="cni">
</div>
<div class="form-group">
<label for="cni">Frais de transport:</label>
<input type="text" class="form-control" name="fraiTransport" id="cni">
</div>
</fieldset>
</div>
<div class="form-group">
<label for="nuveau">Group:</label>
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="group">Options</label>
</div>
<select class="custom-select" id="nuveau" name="group">
<option selected>Choose...</option>
#foreach($groups as $group)
<option value="{{$group->id}}">{{$group->group_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="payment">Payment:</label>
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="payment">Options</label>
</div>
<select class="custom-select" id="payment" name="paymentD">
<option selected>Choose...</option>
#foreach($payment_dures as $payment_dure)
<option value="{{$payment_dure->id}}">{{$payment_dure->payment_dure_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="dateStart"></label>
<input type="date" class="#" id="dateStart" style="width: 49%;" name="edateStart">
<label for="dateEnd">au:</label>
<input type="date" class="#" id="dateEnd" style="width: 44%;" name="edateEnd">
</div>
<div class="form-group">
<label for="MontantP">Montant paye:</label>
<div class="input-group mb-3">
<input type="text" class="form-control" name="MontantP" placeholder="" id="MontantP">
<div class="input-group-append">
<span class="input-group-text">DH</span>
</div>
</div>
</div>
<div class="form-group">
<label for="RestP">Rest a paye paye:</label>
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="" id="MontantR" name="RestP">
<div class="input-group-append">
<span class="input-group-text">DH</span>
</div>
</div>
</div>
<p>
<a class="btn btn-primary" data-toggle="collapse" href="#multiCollapseExample1" role="button" aria-expanded="false" aria-controls="multiCollapseExample1" style="width: 100%">Bus subscribtion</a>
<div class="row">
<div class="col">
<div class="collapse multi-collapse" id="multiCollapseExample1">
<div class="card card-body">
<div class="form-group">
<div class="form-group">
<label for="TMontantP">Montant paye:</label>
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="" id="TMontantP" name="TransportP">
<div class="input-group-append">
<span class="input-group-text">DH</span>
</div>
</div>
</div>
<div class="form-group">
<label for="TRestP">Rest a paye paye:</label>
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="" id="TRestP" name="TransportR">
<div class="input-group-append">
<span class="input-group-text">DH</span>
</div>
</div>
</div>
<div class="form-group">
<label for="TdateStart"></label>
<input type="date" class="#" id="TdateStart" style="width: 48%;" name="TdateStart">
<label for="dTateEnd">au:</label>
<input type="date" class="#" id="TdateEnd" style="width: 44%;" name="TdateEnd">
</div>
</div>
</div>
</div>
</div>
</div>
</p>
<br>
<button type="submit" class="btn btn-secondary">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
#else
<div class="d-flex justify-content-center text-center">
<div class="alert alert-danger" role="alert" style="width: 70%">
identifiƩ vous d'abord
</div>
</div>
#endauth
#endif
#endsection
Looks like your foreign key constraint isn't setup correctly. From the look of the error you have setup payment_agrements.responsible_id to point to payment_agrements.id instead of responsibles.id.
In the migration for that table, that field probably looks like this:
$table->foreign('responsible_id')->references('id')->on('payment_agrements');
When it should be:
$table->foreign('responsible_id')->references('id')->on('responsibles');

how to send data from the view to the controller In laravel

I'm doing a web application, and I get to a point where I can not move forward, I do not know how to do this, I'm starting with laravel so maybe the solution is very simple:
public function detalle($id){
// $nota = App\Nota::find($id);
//AquĆ­ valida si existe sino redirije al 404
$producto = App\Producto::findOrFail($id);
$cantidad = 1;
return view('productos.detalle', compact('producto','cantidad'));
}
View:
<div class="card col-6 ">
<div class="card-body card-buy">
<h1 class="display-4 ">{{$producto->nombre}}</h1>
<p class="h3 mt-5">S/ {{$producto->precio}}</p>
<div class="form-inline">
<i class="fas fa-credit-card h1 mr-1"></i>
<p class="mt-2 "> <b>Metodo de pago:</b> A convenir</p>
</div>
<div class="form-inline">
<i class="fab fa-mailchimp ml-2 mr-2 h1"></i>
<p class="mt-2 "> <b>Envio:</b> Disponible a todo el pais</p>
</div>
<div class="form-inline ">
<div class="mt-n5 mr-2">
<b>Cantidad:</b>
</div>
<div class="def-number-input number-input safari_only">
<button onclick="this.parentNode.querySelector('input[type=number]').stepDown()" class="minus"></button>
<input readonly="readonly" class="quantity" min="1" max="{{$producto->stock}}" name="quantity" value="{{$cantidad}}" type="number">
<button onclick="this.parentNode.querySelector('input[type=number]').stepUp()" class="plus"></button>
</div>
<div class="mt-n5 ml-2">
<b class="text-decoration-none"> ( {{$producto->stock}} disponibles)</b>
</div>
</div>
Comprar
</div>
</div>
https://gyazo.com/febcd4c38cb3ef244e7424a3d92079a4
I already created the detail view of my product, and I put a button for the user to select the amount of products he wants, until there is everything right ...
But I want that number that the person chose to go to the next view, which is to confirm
https://gyazo.com/f56cf578808e75a0fab25eeffa8d2c5d
but that data {{$cantidad}} I do not know how to pass it to this new view I put the code:
Controller:
public function detallescompra($id){
$producto = App\Producto::findOrFail($id);
$total = 0;
$num1 = $producto->precio;
$num2 = $producto->stock;
$total = $num1 * $num2;
return view('productos.compra', compact('producto','total'));
}
and in the view:
<div class="container">
<div class="row" id="contenido">
<div class="col-md-8">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<span>Revisa y Confirma</span>
Seguir viendo...
</div>
<div class="card-body">
#if ( session('mensaje') )
<div class="alert alert-success">{{ session('mensaje') }}</div>
#endif
<form method="POST" action="{{ route ('productos.comprafinal') }}">
#csrf
<label class="text-gray">Contacto y Envio:</label>
<div id="contenido">
<input type="name" name="usuario" placeholder="Nombre y Apellido" class="form-control mb-2" required />
<input type="email" name="email" placeholder="Email de contacto" class="form-control mb-2" required />
<input type="number" name="tlf" placeholder="Numero de telefono" class="form-control mb-2" />
<input type="street-address" name="direccion" placeholder="Direccion (Calle - #Casa - Referencia)" class="form-control mb-2" validate required />
<input type="text" name="id" placeholder="" class="form-control mb-2 d-none" value="{{$producto->id}}" />
<input type="text" name="timestamps" placeholder="" class="form-control mb-2 d-none" value="{{$producto->timestamp}}" />
<label class="text-gray">Producto:</label>
<input type="text" name="nombre" placeholder="Precio del Producto" class="form-control mb-2" value="{{$producto->nombre}}" readonly="readonly" />
<input type="text" name="precio" placeholder="Precio del Producto" class="form-control mb-2" value="Precio por unidad: S/ {{$producto->precio}}" readonly="readonly" id="valor1" />
<input type="text" name="cantidad" placeholder="Precio del Producto" class="form-control mb-2" value="Numero de productos: " readonly="readonly" id="valor2" />
<input type="text" name="" placeholder="Precio del Producto" class="form-control mb-2" value="Total a pagar: S/ {{$total}}" readonly="readonly" id="total" />
<button class="btn btn-success btn-block" type="submit">Comprar</button>
</div>
</form>
</div>
</div>
</div>
<div class="col-mod-4">
<div class="card" style="width: 18rem;">
<img src="{{$producto->img}}" class="card-img-top" alt="...">
</div>
</div>
</div>
I just need to spend that {{$cantidad}} in my view productos.compra, So far I have tried many things but nothing has given me even the minimum result
I'm new to this, I need help :(
GG :D
public function detallescompra(Request $request,$id){
$producto = App\Producto::findOrFail($id);
$cantidad = $request->input('cantidad');
$total = 0;
$num1 = $producto->precio;
$num2 = $cantidad;
$total = $num1 * $num2;
return view('productos.compra', compact('producto','total','cantidad'));
}
View:
<form action="{{ route('productos.compra-confirm', $producto) }}" method="POST">
#csrf
<div class="form-inline ">
<div class="mt-n5 mr-2">
<b>Cantidad:</b>
</div>
<div class="def-number-input number-input safari_only">
<button type="button" onclick="this.parentNode.querySelector('input[type=number]').stepDown()" class="minus"></button>
<input readonly="readonly" class="quantity" min="1" max="{{$producto->stock}}" name="cantidad" value="{{$cantidad}}" type="number">
<button type="button" onclick="this.parentNode.querySelector('input[type=number]').stepUp()" class="plus"></button>
</div>
<div class="mt-n5 ml-2">
<b class="text-decoration-none"> ( {{$producto->stock}} disponibles)</b>
</div>
</div>
<button type="submit" class=" align-bottom btn btn-success btn-lg btn-block ">Comprar</button>
</div>
</div>
</form>
routes web:
Route::post('/comprar/{id}', 'HomeController#detallescompra')->name('productos.compra-confirm');

Resources