I'm trying to update my user in laravel using ajax but I get this message No query results for model [App\User] {id} what it means and How to resolve this error
my controller code is
public function update(Request $request, $id) {
$user = User::findOrFail($id);
$user->name = $request->name;
$user->phone = $request->phone;
$user->email = $request->email;
$user->country = $request->country;
$user->state = $request->state;
$user->purpose = $request->purpose;
$user->package = $request->package;
$user->months = $request->months;
$user->quantity = $request->quantity;
$user->amount = $request->amount;
$user->expiry_date = $request->expiry_date;
$user->save();
return response()->json($user);
}
my blade file code is
<form id="editFormId">
<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">
<h2>Order Information</h2>
<div class="row">
<input type="hidden" value="{{ $userinformations[0]->id }}" name="id" id="id">
<div class="col-md-3">
<div class="form-group">
<label for="package">{{ __('Package') }}</label>
<select id="package" name="package" class="form-control #error('package') is-invalid #enderror" value="{{ old('package') }}" required autocomplete="package" autofocus>
#foreach($userinformations as $userinformation)
<option value="{{ $userinformation->package }}" <?php if(isset($_GET['package']) == '{{ $userinformation->package }}') echo 'selected'; ?>>{{ ucfirst($userinformation->package) }}</option>
#endforeach
<option value="basic">Basic</option>
<option value="regular">Regular</option>
<option value="infinite">Infinite</option>
</select>
#error('package')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="months">{{ __('Months') }}</label>
<select id="months" name="months" class="form-control #error('months') is-invalid #enderror" value="{{ old('months') }}" required autocomplete="months" autofocus>
#foreach($userinformations as $userinformation)
<option value="{{ $userinformation->months }}" <?php if(isset($_GET['months']) == '{{ $userinformation->months }}') echo 'selected'; ?>>{{ $userinformation->months }} Months</option>
#endforeach
<option value="1">1 months</option>
<option value="4">4 months</option>
<option value="12">12 months</option>
</select>
#error('months')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="quantity">{{ __('Quantity') }}</label>
<select id="quantity" name="quantity" class="form-control #error('quantity') is-invalid #enderror" value="{{ old('quantity') }}" required autocomplete="quantity" autofocus>
<option value="1" selected="selected">1</option>
<option value="2">2</option><option value="3">3</option>
<option value="4">4</option><option value="5">5</option>
<option value="6">6</option><option value="7">7</option>
<option value="8">8</option><option value="9">9</option>
<option value="10">10</option>
</select>
#error('quantity')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-3 ">
<label for="amount">{{ __('Amount') }}</label>
<div class="input-group">
<span class="input-group-addon">INR</span>
<select id="amount" name="amount" class="form-control #error('amount') is-invalid #enderror" value="{{ old('amount') }}" required autocomplete="amount" autofocus>
#foreach($userinformations as $userinformation)
<option value="{{ $userinformation->amount }}" <?php if(isset($_GET['amount']) == '{{ $userinformation->amount }}') echo 'selected'; ?>>{{ $userinformation->amount }}</option>
#endforeach
<option>Select Amount</option>
<option value="2500" data-package="basic">2500</option>
<option value="4000" data-package="basic">4000</option>
<option value="7000" data-package="basic">7000</option>
<option value="3500" data-package="regular">3500</option>
<option value="7000" data-package="regular">7000</option>
<option value="12000" data-package="regular">12000</option>
<option value="15000" data-package="infinite">15000</option>
<option value="30000" data-package="infinite">30000</option>
<option value="50000" data-package="infinite">50000</option>
</select>
</div>
</div>
</div>
<hr>
<h2>Billing Information</h2>
<div class="row">
<div class="col-md-6 form-group">
<label for="name">{{ __('Name') }}</label>
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ $userinformations[0]->name }}" required autocomplete="name" autofocus disabled>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-md-6 form-group">
<label for="phone">{{ __('Phone') }}</label>
<input id="phone" type="text" class="form-control #error('phone') is-invalid #enderror" name="phone" value="{{ $userinformations[0]->phone }}" required autocomplete="phone" disabled>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label for="country">{{ __('Country') }}</label>
<input id="country" type="text" class="form-control #error('country') is-invalid #enderror" name="country" value="{{ $userinformations[0]->country }}" required autocomplete="country" autofocus disabled>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-md-6 form-group">
<label for="state">{{ __('State') }}</label>
<input id="state" type="text" class="form-control #error('state') is-invalid #enderror" name="state" value="{{ $userinformations[0]->state }}" required autocomplete="state" autofocus disabled>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="row">
<div class="col-md-6 form-group">
<label for="purpose">{{ __('Purpose') }}</label>
<select id="purpose" class="form-control #error('purpose') is-invalid #enderror" name="purpose" value="{{ old('purpose') }}">
#foreach($userinformations as $userinformation)
<option value="{{ $userinformation->purpose }}" <?php if(isset($_GET['purpose']) == '{{ $userinformation->purpose }}') echo 'selected'; ?>>{{ $userinformation->purpose }}</option>
#endforeach
<!---option>Select Purpose</option>
<option value="parent">Parent</option>
<option value="employmee">Employmee</option---->
</select>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-md-6 form-group">
<label for="email">{{ __('E-Mail Address') }}</label>
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ $userinformations[0]->email }}" required autocomplete="email" disabled>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<!---button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button---->
Make Payment
</div>
</div>
</form>
route is
Route::put('update/{id}', 'razorpay\PaymentController#update');
My ajax request is
<script>
var SITEURL = 'http://localhost:8000/update/{user}';
//alert('SITEURL');
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('body').on('click', '.buy_now', function(e){
var totalAmount = $(this).attr("data-amount");
var product_id = $(this).attr("data-id");
var options = {
"key": "***********",
"amount": (totalAmount*100), // 2000 paise = INR 20
"name": "AddSpy",
"description": "Payment",
"image": "http://localhost:8000/frontend/images/addspy1.png",
"handler": function (response){
$.ajax({
url: SITEURL,
type: 'post',
dataType: 'json',
data: {
"_token": $('#token').val(),
razorpay_payment_id: response.razorpay_payment_id ,
totalAmount : totalAmount ,product_id : product_id,
name : $('#name').val(),
phone : $('#phone').val(),
email : $('#email').val(),
country : $('#country').val(),
state : $('#state').val(),
purpose : $('#purpose').val(),
package : $('#package').val(),
months : $('#months').val(),
quantity : $('#quantity').val(),
amount : $('#amount').val(),
password : $('#password').val(),
expiry_date : $('#expiry_date').val(),
},
success: function (msg) {
//window.location.href = 'thank-you';
}
});
},
"prefill": {
"contact": '1234567890',
"email": 'xxxxxxxxx#gmail.com',
},
"theme": {
"color": "#528FF0"
}
};
var rzp1 = new Razorpay(options);
rzp1.open();
e.preventDefault();
});
</script>
please check my code and update it. Let me please know where I'm wrong and how correct it.
Thanks in advance
You have to change {user} by the desired UserID that you want to update:
var SITEURL = 'http://localhost:8000/update/{user}';
Also, your code can be improved like this:
public function update(Request $request, User $user) {
// $user = User::findOrFail($id); -- removed
$user->name = $request->name;
$user->phone = $request->phone;
$user->email = $request->email;
$user->country = $request->country;
$user->state = $request->state;
$user->purpose = $request->purpose;
$user->package = $request->package;
$user->months = $request->months;
$user->quantity = $request->quantity;
$user->amount = $request->amount;
$user->expiry_date = $request->expiry_date;
$user->save();
return response()->json($user);
}
Route::put('update/{user}', 'razorpay\PaymentController#update');
Related
I cant login to my system, I don't what is the problem. It doesn't show any errors but only a 404 page and this URL:
here is my login blade
<form action="POST" action="{{ route('login') }}">
#csrf
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" id="email" class="form-control #error('email') is-invalid #enderror" placeholder="email#example.com" value="{{ old('email') }}">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group mb-4">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control #error('password') is-invalid #enderror" placeholder="masukkan password" required autocomplete="current-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<button type="submit" class="btn btn-block login-btn" action="{{ route('landpage') }}">
Login
</button>
</form>
controller
use AuthenticatesUsers;
public function redirectTo(){
// User role
$role = Auth::user()->role;
// Check user role
switch ($role) {
case 'student':
return 'backend/profilone';
break;
case 'staff':
return 'backend/profiltwo';
break;
default:
return '/home';
break;
}
}
web.php
Route::group(['middleware' => ['auth', 'checkRole:superadmin,admin']], function(){
Route::get('/home', 'Backend\HomeController#index')->name('home');
});
<form method="POST" action="{{ route('login') }}">
#csrf
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" id="email" class="form-control #error('email') is-invalid #enderror" placeholder="email#example.com" value="{{ old('email') }}">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group mb-4">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control #error('password') is-invalid #enderror" placeholder="masukkan password" required autocomplete="current-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<button type="submit" class="btn btn-block login-btn" action="{{ route('landpage') }}">
Login
</button>
</form>
Route::group(['middleware' => ['auth', 'checkRole:superadmin,admin']], function(){
Route::get('/home', 'Backend\HomeController#index')->name('home');
Route::post('/login', 'Backend\HomeController#methodforlogin')->name('login');
});
)
I have been struggling with a form for my Laravel App....
I have 3 fields which are checkboxes and therefore is getting POSTed as arrays and then I have a textarea field and somehow SOMETIMES one of the 3 checkbox inputs get flooded with data from the textarea input field... It is very weird and only happens sometime and therefore I get a data to long truncated error in the database...
But I DD the input at the $request level and can see already there the array from the checkbox is flooded with the data from the textarea field... I can not wrap my head around why this is, as it's only sometimes...
And I can even get the error and go back a page in the browser and submit again with all the same data and then it can be working.. WHAT can cause this weird behavioer
This is my Laravel blade where the form is
#extends('layouts.app')
#section('head')
#endsection
#section('content')
#php
// dd($errors);
#endphp
<h2 class="page-title">Opret Artist Side</h2>
<div class="artist-box">
<form class="formgrid" method="POST" action="{{ route('artist.store') }}">
#csrf
<div class="formleft">
<label for="artist_name" class="label">Artist navn</label>
<input type="text" id="artist_name" name="artist_name" class="formitem #error('artist_name') is-invalid #enderror" value="{{ old('artist_name', $post->artist_name ?? null) }}" />
#error('artist_name')
<div class="invalid-feedback">
#foreach($errors->get('artist_name') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="artist_name_help">Jeres band/artist navn. <span class="text-danger">OBS! Kan IKKE ændres</span></small>
#enderror
</div>
<div class="formright">
<label for="contact_person" class="label">Kontakt person</label>
<input type="text" name="contact_person" id="contact_person" class="formitem #error('contact_person') is-invalid #enderror" value="{{ old('contact_person', $post->content ?? null) }}" />
#error('contact_person')
<div class="invalid-feedback">
#foreach($errors->get('contact_person') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="contact_person_help">Kontakt persons navn</small>
#enderror
</div>
<div class="formleft">
<label for="email" class="label">Email</label>
<input type="text" name="email" id="email" class="formitem #error('email') is-invalid #enderror" value="{{ old('email', $post->email ?? null) }}" />
#error('email')
<div class="invalid-feedback">
#foreach($errors->get('email') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="email_help">Kontakt email</small>
#enderror
</div>
<div class="formright">
<label for="phone" class="label">Tlf nummer</label>
<input type="text" name="phone" id="phone" class="formitem #error('phone') is-invalid #enderror" value="{{ old('phone', $post->phone ?? null) }}" />
#error('phone')
<div class="invalid-feedback">
#foreach($errors->get('phone') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="phone_help">Kontakt tlf nummer</small>
#enderror
</div>
<div class="formleft">
<label for="hometown" class="label">Hjemby</label>
<input type="text" name="hometown" id="hometown" class="formitem #error('hometown') is-invalid #enderror" value="{{ old('hometown', $post->hometown ?? null) }}" />
#error('hometown')
<div class="invalid-feedback">
#foreach($errors->get('hometown') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="hometown_help">Jeres Hjemby</small>
#enderror
</div>
<div class="formright">
<label for="category" class="label">Kategori</label>
<select name="category" id="category" class="formitem #error('category') is-invalid #enderror">
{{-- Show a non selectable if with info of none selected --}}
#if(old('category') === null)
<option disabled="disabled" selected value="null">Vælg en kategori</option>
#endif
#foreach($categories as $category)
<option #if(old('category') !== null && old('category') == $category->id) selected #endif value="{{ $category->id }}"
>{{ $category->artist_category }}</option>
#endforeach
</select>
#error('category')
<div class="invalid-feedback">
#foreach($errors->get('category') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="">Hvilken kategori hører i under</small>
#enderror
</div>
<div class="formfull">
<label for="budget" class="label">Budget i spiller for</label>
<ul class="ks-cboxtags">
#foreach($budgets as $budget)
{{-- <div class="form-check form-check-inline"> --}}
<li><input #if(old('budget') !== null && in_array($budget->id,old('budget'))) checked #endif
type="checkbox" name="budget[]" id="budget{{ $budget->id }}" class="form-check-input #error('budget') is-invalid #enderror" value="{{ $budget->id }}" />
<label class="form-check-label" for="budget{{ $budget->id }}">{{ $budget->artist_budget }}</label></li>
{{-- </div> --}}
#endforeach
</ul>
#error('budget')
<div class="invalid-feedback d-block">
#foreach($errors->get('budget') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="budget_help">Vælg hvilket budget i optræder for, gerne flere</small>
#enderror
</div>
{{-- --}}
<div class="formfull">
<label for="genres" class="label">Genre</label>
<ul class="ks-cboxtags">
#foreach($genres as $genre)
{{-- <div class="form-check form-check-inline"> --}}
<li><input #if(old('genres') !== null && in_array($genre->id,old('genres'))) checked #endif
type="checkbox" name="genres[]" id="genres{{ $genre->id }}" value="{{ $genre->id }}" class="form-check-input #error('genres') is-invalid #enderror" />
<label class="form-check-label" for="genres{{ $genre->id }}">{{ $genre->genre }}</label></li>
{{-- </div> --}}
#endforeach
</ul>
#error('genres')
<div class="invalid-feedback d-block">
#foreach($errors->get('genres') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="">Hvike genrer hører i under</small>
#enderror
</div>
{{-- --}}
<div class="formfull">
<label for="area" class="label">Områder hvor i optræder</label>
<ul class="ks-cboxtags">
#foreach($areas as $area)
{{-- <div class="form-check form-check-inline"> --}}
<li><input #if(old('area') !== null && in_array($area->id,old('area'))) checked #endif
type="checkbox" name="area[]" id="area{{ $area->id }}" value="{{ $area->id }}" class="form-check-input #error('area') is-invalid #enderror" />
<label class="form-check-label" for="area{{ $area->id }}">{{ $area->artist_area }}</label></li>
{{-- </div> --}}
#endforeach
</ul>
#error('area')
<div class="invalid-feedback d-block">
#foreach($errors->get('area') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="">Vælg hvilke områder i optræder i</small>
#enderror
</div>
<div class="formleft">
<label for="desc" class="label">Beskriv jer selv</label>
<textarea name="description" id="description" class="formitem #error('description') is-invalid #enderror">{{ old('description', $post->description ?? null) }}</textarea>
{{-- <input id="description" value="{{ old('description', $post->description ?? null) }}" class="form-control #error('description') is-invalid #enderror" name="description" type="hidden">
#trix(\App\ArtistPage::class, 'trixinput',
[
'id' => 'description',
// 'class' => 'form-control',
'hideTools' => ['file-tools'],
'hideButtonIcons' => ['attach', 'link', 'code', 'strike', 'heading-1']
]) --}}
{{-- <input id="desc" value="{{ old('description', $post->description ?? null) }}" type="hidden" name="description">
<trix-editor input="desc" class="#error('description') is-invalid #enderror"></trix-editor> --}}
#error('description')
<div class="invalid-feedback">
#foreach($errors->get('description') as $error)
{{ $error }}
#endforeach
</div>
#else
<small class="text-muted form-text" id="description_help">Giv en god beskrivelse af jer selv og hvad i tilbyder<br>
Tilladte tags er <strong>{{ '<br><b><i><li><ul><ol><hr><strong><p>' }}</strong>
</small>
#enderror
</div>
<button type="submit" class="btn btn-primary">Opret</button>
</form>
</div>
#endsection
See also this image of the DD dump on the $request
Hmmmm Think I figured it out :-)
But it has been such a sporadic bug so I'm not sure yet... But seems like it has gone away :-)
Seems like it was this value line on each field I have gotten messed up
The value line structure on the input fields I had was
value="{{ old('artist_name', $post->artist_name ?? null) }}"
And it should be
value="{{ old('artist_name') ?? $post->artist_name ?? null }}"
Dont know why I ended up with such an obscure Value part on every input field :-)
But sometimes the eyes just gets blind on code :-)
In my Laravel-5.8 project consisting of Sales application, I have this codes:
Controller:
public function create()
{
$suppliers =Supplier::all();
$categories = Category::all();
$taxes = Tax::all();
$units = Unit::all();
return view('product.create', compact('categories','taxes','units','suppliers'));
}
public function store(Request $request)
{
$request->validate([
'name' => 'required|min:3|unique:products|regex:/^[a-zA-Z ]+$/',
'serial_number' => 'required',
'model' => 'required|min:3',
'category_id' => 'required',
'sales_price' => 'required',
'unit_id' => 'required',
'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'tax_id' => 'required',
]);
$product = new Product();
$product->name = $request->name;
$product->serial_number = $request->serial_number;
$product->model = $request->model;
$product->category_id = $request->category_id;
$product->sales_price = $request->sales_price;
$product->unit_id = $request->unit_id;
$product->tax_id = $request->tax_id;
if ($request->hasFile('image')){
$imageName =request()->image->getClientOriginalName();
request()->image->move(public_path('images/product/'), $imageName);
$product->image = $imageName;
}
$product->save();
foreach($request->supplier_id as $key => $supplier_id){
$supplier = new ProductSupplier();
$supplier->product_id = $product->id;
$supplier->supplier_id = $request->supplier_id[$key];
$supplier->price = $request->supplier_price[$key];
$supplier->save();
}
return redirect()->back()->with('message', 'Product Created Successfully');
}
view
<div class="row mt-2">
<div class="clearix"></div>
<div class="col-md-10">
<div class="tile">
<h3 class="tile-title">Product</h3>
<div class="tile-body">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form method="POST" action="{{route('product.store')}}" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="form-group col-md-6">
<label class="control-label">Product Name</label>
<input name="name" class="form-control #error('name') is-invalid #enderror" type="text" placeholder="Product Name">
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Serial Number</label>
<input name="serial_number" class="form-control #error('serial_number') is-invalid #enderror" type="number" placeholder="Enter Tax Name">
#error('serial_number')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Model</label>
<input name="model" class="form-control #error('name') is-invalid #enderror" type="text" placeholder="Enter Tax Name">
#error('model')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Category</label>
<select name="category_id" class="form-control">
<option>---Select Category---</option>
#foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
#error('category_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Sale Price</label>
<input name="sales_price" class="form-control #error('sales_price') is-invalid #enderror" type="number" placeholder="Enter Tax Name">
#error('sales_price')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Unite</label>
<select name="unit_id" class="form-control">
<option>---Select Unit---</option>
#foreach($units as $unit)
<option value="{{$unit->id}}">{{$unit->name}}</option>
#endforeach
</select>
#error('unit_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Image</label>
<input name="image" class="form-control #error('image') is-invalid #enderror" type="file" >
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group col-md-6">
<label class="control-label">Tax </label>
<select name="tax_id" class="form-control">
<option>---Select Tax---</option>
#foreach($taxes as $tax)
<option value="{{$tax->id}}">{{$tax->name}} %</option>
#endforeach
</select>
#error('tax_id')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="tile">
<div id="example-2" class="content">
<div class="group row">
<div class="form-group col-md-5">
<select name="supplier_id[]" class="form-control">
<option>Select Supplier</option>
#foreach($suppliers as $supplier)
<option value="{{$supplier->id}}">{{$supplier->name}} </option>
#endforeach
</select>
</div>
<div class="form-group col-md-5">
<input name="supplier_price[]" value="{{old('supplier_price')}}" class="form-control #error('supplier_price') is-invalid #enderror" type="number" placeholder="Enter Sales Price">
<span class="text-danger">{{ $errors->has('additional_body') ? $errors->first('body') : '' }}</span>
</div>
<div class="form-group col-md-2">
<button type="button" id="btnAdd-2" class="btn btn-primary float-right"><i class="fa fa-plus"></i></button>
<button type="button" class="btn btn-danger btnRemove float-right"><i class="fa fa-trash"></i></button>
</div>
</div>
</div>
</div>
<div class="form-group col-md-4 align-self-end">
<button class="btn btn-primary" type="submit"><i class="fa fa-fw fa-lg fa-check-circle"></i>Update</button>
</div>
</form>
</div>
</div>
</div>
</div>
When I submitted the form, I got this error:
htmlspecialchars() expects parameter 1 to be string, array given (View:...
Then when I removed this:
value="{{old('supplier_price')}}"
from:
<div class="form-group col-md-5">
<input name="supplier_price[]" value="{{old('supplier_price')}}" class="form-control #error('supplier_price') is-invalid #enderror" type="number" placeholder="Enter Sales Price">
<span class="text-danger">{{ $errors->has('additional_body') ? $errors->first('body') : '' }}</span>
</div>
the error vanished.
I wanted to use it for the input field to retain its value after any error in validation. Where did I get it wrong and how do I correct it?
Thank you
supplier_price is an array since you define it as name="supplier_price[]"
your old() function will output an array while the mustache function {{}} expects a string.
If I understand your code properly you should be better off removing the [] from name="supplier_price[]"
I have the following for loop
#for($i = 0; $i < $contactAmount; $i++)
<div class="form-group row">
<label for="firstname" class="col-md-4 col-form-label text-md-right">voornaam</label>
<div class="col-md-6">
<input id="firstname" type="text" class="form-control #error('firstname.0') is-invalid #enderror" name="firstname[]" value="{{ old('firstname.0') }}" autofocus>
#error('firstname.0')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
#endfor
but if I replace the hard coded '0' with {{ $i }} my validation does not work anymore. So how do I call my integer and use it inside laravel helper functions?
#for($i = 0; $i < $contactAmount; $i++)
<div class="form-group row">
<label for="firstname" class="col-md-4 col-form-label text-md-right">voornaam</label>
<div class="col-md-6">
<input id="firstname" type="text" class="form-control #error('firstname.{{ $i }}') is-invalid #enderror" name="firstname[]" value="{{ old('firstname.'. $i) }}" autofocus>
#error('firstname.{{ $i }}')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
#endfor
Why don't you use a #foreach? You can access the index of the loop as follows:
#foreach($contactAmount as $amount)
<div class="form-group row">
{{ $loop->iteration }}
...
</div>
#endforeach
I want to show the data after user have selected the project.
I've tried several method but none of them can show the next data.
I want to create as simple as when user choose a project, it'll show the next question such as select product, select period, etc.
this is my form.blade.php
#extends('layouts.master')
#section('style')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheer" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style type="text/css">
.box{
width:600px;
margin:0 auto;
border:1[x solid #ccc;]
}
</style>
#endsection
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Add Prospect</div>
<form action="{{ url('/add/outlet') }}" method="post" enctype="multipart/form-data">
#csrf
<div class="card-body">
<div class="form-group">
<select name="project_name" id="project" class="form-control input-lg dynamic" data-dependent="product">
<option value="">Select Project</option>
#foreach($project_list as $project)
<option value="{{$project->project}}"> {{$project->project}} </option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="product_name" id="product" class="form-control input-lg">
<option value="">Select Product</option>
<option value="HelloBill Retail">HelloBill Retail</option>
<option value="HelloBill FNB">HelloBill FNB</option>
</select>
</div>
<div class="form-group">
<select name="period" id="period" class="form-control input-lg">
<option value="">Select Period</option>
<option value="Bulanan">Bulanan</option>
<option value="Tahunan">Tahunan</option>
</select>
</div>
<div class="form-group row">
<label for="outlet" class="col-md-4 col-form-label text-md-left">Outlet</label>
<div class="col-md-8">
<input id="outlet" type="text" class="form-control{{ $errors->has('outlet') ? ' is-invalid' : '' }}" name="outlet" value="{{ old('outlet') }}" required autofocus>
#if ($errors->has('outlet'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('outlet') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="alamat" class="col-md-4 col-form-label text-md-left">Alamat</label>
<div class="col-md-8">
<input id="addressLine1" type="text" class="form-control{{ $errors->has('addressLine1') ? ' is-invalid' : '' }}" name="addressLine1" value="{{ old('addressLine1') }}" placeholder="Address Line #1" required autofocus>
#if ($errors->has('addressLine1'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('addressLine1') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-left"></label>
<div class="col-md-8">
<input id="addressLine2" type="text" class="form-control" name="addressLine2" value="{{ old('addressLine2') }}" placeholder="Address Line #2" required autofocus>
</div>
</div>
<div class="form-group row">
<label for="kelurahan" class="col-md-4 col-form-label text-md-left"></label>
<div class="col-md-8">
<input id="kelurahan" type="text" class="form-control{{ $errors->has('kelurahan') ? ' is-invalid' : '' }}" name="kelurahan" value="{{ old('kelurahan') }}" placeholder="Kelurahan" required autofocus>
#if ($errors->has('kelurahan'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('kelurahan') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="kecamatan" class="col-md-4 col-form-label text-md-left"></label>
<div class="col-md-8">
<input id="kecamatan" type="text" class="form-control{{ $errors->has('kecamatan') ? ' is-invalid' : '' }}" name="kecamatan" value="{{ old('kecamatan') }}" placeholder="Kecamatan" required autofocus>
#if ($errors->has('kecamatan'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('kecamatan') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="kota" class="col-md-4 col-form-label text-md-left"></label>
<div class="col-md-8">
<input id="kota" type="text" class="form-control{{ $errors->has('kota') ? ' is-invalid' : '' }}" name="kota" value="{{ old('kota') }}" placeholder="Kota" required autofocus>
#if ($errors->has('kota'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('kota') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="provinsi" class="col-md-4 col-form-label text-md-left"></label>
<div class="col-md-8">
<input id="provinsi" type="text" class="form-control{{ $errors->has('provinsi') ? ' is-invalid' : '' }}" name="provinsi" value="{{ old('provinsi') }}" placeholder="Provinsi" required autofocus>
#if ($errors->has('provinsi'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('provinsi') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="zipCode" class="col-md-4 col-form-label text-md-left"></label>
<div class="col-md-8">
<input id="zipCode" type="text" class="form-control{{ $errors->has('zipCode') ? ' is-invalid' : '' }}" name="zipCode" value="{{ old('zipCode') }}" placeholder="Kode Pos" required autofocus>
#if ($errors->has('zipCode'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('zipCode') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="PIC" class="col-md-4 col-form-label text-md-left">PIC</label>
<div class="col-md-8">
<input id="PIC" type="text" class="form-control{{ $errors->has('PIC') ? ' is-invalid' : '' }}" name="PIC" value="{{ old('PIC') }}" required autofocus>
#if ($errors->has('PIC'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('PIC') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-left">Email</label>
<div class="col-md-8">
<input id="email" type="text" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Add
</button>
</div>
</div>
#if($errors->any())
<div class="category-msg-error">{{ $errors->first() }}</div>
#endif
</div>
</form>
</div>
</div>
</div>
</div>
#endsection
#section('extra')
<script>
$(document).ready(function(){
$('.dynamic').change(function(){
if($(this).val()!=''){
var select = $(this).attr("id");
var value = $(this).val();
var dependent = $(this).data('dependent');
var __token = $('input[name=_token"]').val();
$.ajax({
url:"{{route('dynamicdependent.fetch')}}",
method:"POST",
data:{select:select, value:value, _token:_token,
dependent=dependent},
success:function(result){
$('#+dependent').html(result);
}
})
}
});
});
</script>
#endsection
please help. and i am also new in Laravel
There are two mistakes:-
In ajax request data you are setting dependent=dependent that should be dependent: dependent.
In Success response, you are setting this $('#+dependent').html(result); but that is not correct. It should be this $('#'+dependent).html(result);
Below is the full jQuery code:
<script>
$(document).ready(function(){
$('.dynamic').change(function(){
if($(this).val()!=''){
var select = $(this).attr("id");
var value = $(this).val();
var dependent = $(this).data('dependent');
var __token = $('input[name=_token"]').val();
$.ajax({
url:"{{route('dynamicdependent.fetch')}}",
method:"POST",
data:{
select:select,
value:value,
_token:_token,
dependent:dependent
},
success:function(result){
$('#'+dependent).html(result);
}
});
}
});
});
</script>
Please check and let me know if that will not help. I will try to solve your problem.