Button can't save first item when clicked - laravel

In a foreach loop, I have more than one item and each item has a button called "save". However, when I click on the first "save" button, it saves only the last item in my database.
#foreach($product_dr as $product)
<tr>
<td style="font-size: 20px;color: #101010">
<div class="form-group">
<input type="hidden" name="title" value="{{$product->title}}">
<label for="inputText3" class="col-form-label"></label>
{{$product->title}}
</div>
</td>
<td style="font-size: 20px;color: #2ec551">
<div class="form-group">
<input id="inputText4" VALUE="{{$product->price}}" type="text" name="price" class="form-control" placeholder="Amount" style="width: 100px">
</div>
</td>
<td style="font-size: 20px;color: #2ec551">
<div class="form-group">
<input id="inputText4" VALUE="1" type="number" name="amount" class="form-control" placeholder="Amount" style="width: 100px">
</div>
</td>
<td style="font-size: 20px;color: #2ec551">
<div class="form-group">
<input type="hidden" name="table" value="{{$table[0]->number}}">
<input type="hidden" name="table_number" value="{{$table[0]->number}}">
<label for="inputText3" class="col-form-label"></label>
{{$table[0]->number}}
</div>
</td>
<td>
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12 ">
<button class="btn btn-success" type="submit">Save</button>
</div>
</td>
</tr>
#endforeach

The name of the variables must be an array as such:
<input type="hidden" name="title[]" value="{{$product->title}}">
This way you will have an array on your backed that you can iterate. If not, you will only get one value for "title"

Related

Spring MVC editable table (Thymeleaf)

I have 2 entities - Invoice and InvoiceProduct. Invoice class has a field List of InvoiceProduct because Invoice has #OneToMany mapping on InvoiceProduct.
I want to send the List of InvoiceProduct to Thymeleaf with with editable fields with a Submit button. When I click on Submit button, the new edited List should be returned to the controller. However, I am able to send the list to the view, but when I send the edited list to controller, it is being passed as 'null'.
update-invoice.html:
<form action="#" th:action="#{/api/invoice/saveInvoice}" th:object="${invoice}" method="POST">
<table class="table table-bordered table-striped">
<thead class="thead-dark">
<tr>
<th>PID</th>
<th>Quantity</th>
<th>Unit Price</th>
</tr>
</thead>
<tbody>
<tr th:each="temp : ${invoice?.invoiceProducts}" >
<td><input name="invoiceProducts[${tempStat.index}].productId" th:value="${temp.productId}"/></td>
<td><input name="invoiceProducts[${tempStat.index}].quantity" th:value="${temp.quantity}"/></td>
<td><input name="invoiceProducts[${tempStat.index}].price" th:value="${temp.price}"/></td>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-info col-2">Submit</button>
</form>
InvoiceController:
#PostMapping("/saveInvoice")
public String postInvoice(#ModelAttribute("invoice") Invoice invoice) {
logger.info("invoice products " + invoice.getInvoiceProducts());
return "dummy";
}
What else do I need to add so that my update-invoice.html passes the list to my controller ?
You can try something like below as an example for thymleaf:-
<form method="post" th:action="#{/users/}" th:object="${userInfo}" class="col card p-3 mb-5">
<div class="form-group">
<label for="firstName">First Name</label>
<input id="firstName" placeholder="Enter First Name" required type="text" th:field="*{firstName}"
class="form-control"/>
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input id="lastName" placeholder="Enter Last Name" required type="text" th:field="*{lastName}"
class="form-control"/>
</div>
<div class="form-group">
<label for="role">Role</label>
<select id="role" required th:field="*{role}" class="form-control ">
<option value="" hidden>Select a role</option>
<option th:each="role : ${T(com.springhow.examples.springboot.thymeleaf.domain.entities.Role).values()}"
th:value="${role}"
th:text="${role}">
</option>
</select>
</div>
<input type="submit" class="btn btn-primary" value="Create User">
</form>
and into Controller:-
#RequestMapping(value = "/", method = RequestMethod.POST)
public String createUser(Model model, #ModelAttribute UserInfo userInfo) {
UserInfo user = userService.createUser(userInfo);
return "redirect:/users/";
}

How to pass data to a form in laravel 8

This is how I fill my data in a table body:
<tbody>
#foreach($ArrayProducts as $product)
<tr>
<td>{{$product['name']}}</td>
<td>{{$product['description']}}</td>
<td>{{$product['price']}}</td>
<td>{{$product['stock']}}</td>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Edit</td>
</tr>
#endforeach
</tbody>
But how I can pass the object that is selected in the button Edit of the product to a form to update the product:
<form method="PUT" action="productos">
#csrf
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" id="nombre" name="name" required>
</div>
<div class="form-group">
<label >Descripcion</label>
<input type="text" class="form-control" id="description" name="description" required>
</div>
<div class="form-group">
<label>price</label>
<input id="price" type="number" class="form-control" name="price" required>
</div>
<div class="form-group">
<label>Stock</label>
<input id="stock" type="number" class="form-control" name="stock" required >
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
You will also want to modify this so you know the ID of the product you are editing and do error handling.
<tbody>
#foreach($ArrayProducts as $product)
<tr>
<td>{{$product['name']}}</td>
<td>{{$product['description']}}</td>
<td>{{$product['price']}}</td>
<td>{{$product['stock']}}</td>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#product-model-{{ $product['id'] }}">Edit</button></td>
</tr>
#endforeach
</tbody>
#foreach($ArrayProducts as $product)
<div class="model" id="product-model-{{ $product['id'] }}">
<form method="PUT" action="productos">
#csrf
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" id="nombre" name="name" required value="{{ $product['name'] }}">
</div>
<div class="form-group">
<label >Descripcion</label>
<input type="text" class="form-control" id="description" name="description" required value="{{ $product['description'] }}">
</div>
<div class="form-group">
<label>price</label>
<input id="price" type="number" class="form-control" name="price" required value="{{ $product['price'] }}">
</div>
<div class="form-group">
<label>Stock</label>
<input id="stock" type="number" class="form-control" name="stock" required value="{{ $product['stock'] }}">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
#endforeach

Laravel - How to apply Laravel old() helper function on dynamic input form

In Laravel-5.8 project, I am working on dynamic input form.
The main model is AppraisalGoal while the second model is AppraisalGoalDetail
Controller
public function create()
{
$goal = new AppraisalGoal();
$goaldetail = new AppraisalGoalDetail();
return view('appraisal.appraisal_goals.create')
->with('goal', $goal)
->with('goaldetail', $goaldetail) ;
}
public function store(StoreAppraisalGoalRequest $request)
{
DB::beginTransaction();
try {
$goal = new AppraisalGoal();
$goal->weighted_score = $request->weighted_score;
$goal->goal_title = $request->goal_title;
$goal->goal_description = $request->goal_description;
if ($request->appraisal_doc != "") {
$appraisal_doc = $request->file('appraisal_doc');
$new_name = rand() . '.' . $appraisal_doc->getClientOriginalExtension();
$appraisal_doc->move(public_path('storage/documents/appraisal_goal'), $new_name);
$goal->appraisal_doc = $new_name;
}
$goal->save();
foreach ( $request->activity as $key => $activity){
$startDate = Carbon::parse($request->start_date[$key]);
$endDate = Carbon::parse($request->end_date[$key]);
$insert_array = [
'kpi_description' => $request->kpi_description[$key],
'activity' => $request->activity[$key],
'start_date' => $startDate ->toDateTimeString(),
'end_date' => $endDate->toDateTimeString(),
];
AppraisalGoalDetail::create($insert_array );
}
DB::commit();
Session::flash('success', 'Goal is created successfully');
return redirect()->route(goals.index');
} catch (Exception $exception) {
DB::rollback();
Session::flash('error', 'Action failed! Please try again');
return redirect()->route('goals.index');
}
}
the create.blade view is shown below
<form action="{{route('goals.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"> Goal Title:<span style="color:red;">*</span></label>
<input type="text" name="goal_title" value="{{ old('goal_title', $goal->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" value="{{old('goal_description',$goal->goal_description)}}" placeholder="Enter Goal Description here ...">{{old('goal_description',$goal->goal_description)}}</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<span style="color:red;">*</span></th>
<th scope="col">Start Date<span style="color:red;">*</span></th>
<th scope="col">End Date<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><input type="text" name="activity[]" class="form-control activity" ></td>
<td><input type="text" name="kpi_description[]" class="form-control kpi_description" ></td>
<td><input type="date" class="form-control start_date" placeholder="dd/mm/yyyy" name="start_date[]" min="{{Carbon\Carbon::now()->firstOfYear()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}"></td>
<td><input type="date" class="form-control end_date" placeholder="dd/mm/yyyy" name="end_date[]" min="{{Carbon\Carbon::now()->firstOfYear()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}"></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-6">
<div class="form-group">
<label class="control-label"> Weight(%):<span style="color:red;">*</span></label>
<input type="number" name="weighted_score" placeholder="Enter weighted score here" class="form-control" max="120">
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label class="control-label"> Attachment:</label>
<div class="custom-file">
<input value="{{old('appraisal_doc',$goal->appraisal_doc)}}" type="file" name="appraisal_doc" class="custom-file-input" id="customFile">
<label class="custom-file-label" for="exampleInputFile">Choose file</label>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
</div>
</form>
AppraisalGoal is foreignkey to AppraisalGoalDetail. AppraisalGoalDetail is an Array.
The way the application operates is that, When the user clicks the submit button, the application saves into AppraisalGoal and pick its id and saves it with the other data into AppraisalGoalDetail.
However, the validation is giving issue. Whenever the user submits and the page is validated, all went blank upon showing the error page, meaning that I need to input them all over again.
I was able to resolve these ones that belong to AppraisalGoal model by using old() help function and it works:
<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" value="{{ old('goal_title', $goal->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" value="{{old('goal_description',$goal->goal_description)}}" placeholder="Enter Goal Description here ...">{{old('goal_description',$goal->goal_description)}}</textarea>
</div>
</div>
I don't know how to resolve these ones that belong to AppraisalGoalDetail:
<tr>
<td><input type="text" name="activity[]" class="form-control activity" ></td>
<td><input type="text" name="kpi_description[]" class="form-control kpi_description" ></td>
<td><input type="date" class="form-control start_date" placeholder="dd/mm/yyyy" name="start_date[]" min="{{Carbon\Carbon::now()->firstOfYear()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}"></td>
<td><input type="date" class="form-control end_date" placeholder="dd/mm/yyyy" name="end_date[]" min="{{Carbon\Carbon::now()->firstOfYear()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}"></td>
<td><a class="btn btn-danger remove"> <i class="fa fa-times"></i></a></td>
</tr>
How do I get this corrected that the page should still retain the data after submit and validation error?
Thank you
When You get Validation Error & something fault in server..You redirect the page & withInput() function like this..
return redirect()->route('goals.index')->withInput(['goal_title' => $request->goal_title, 'goal_description' => $request->goal_description]);
It will sork & show your value which you give to update

Controller Update method is not working in laravel

I am using laravel 5.2. Controller method store, show, and edit is working fine but update method is not working.
edit.blade is as:
#extends ('layouts.backend')
#section ('content')
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('backend.index') }}"> Back</a>
</div>
<form method="PATCH" action="{{ route('backend.update', $reports->id) }}">
<div>
create report
</div>
<div>
<table>
<tr>
<td>
<div class="form-group required">
<label for="category_id" class="control-label">ID</label>
<input class="form-control" name="id" id="id" type="text" value="{{ $reports->id}}">
</div>
<div class="form-group required">
<label for="category_id" class="control-label">Category</label>
<select class="form-control" id="category" name="category"><option value="" selected="selected">Select</option><option value="1">Agriculture</option><option value="2">Food</option><option value="3">Beverage</option></select>
</div>
<div class="form-group required">
<label for="name" class="control-label">Name</label>
<input class="form-control" name="name" id="name" type="text" value="{{ $reports->name}}">
</div>
<div class="form-group required">
<label for="url" class="control-label">Url</label>
<input class="form-control" name="slug" id="slug" type="text" value="{{ $reports->slug}}">
</div>
<div class="form-group required">
<label>Brief Description</label></br>
<textarea id="brief_des" name="brief_des" rows="12" cols="70" style="width: 80%" >
{{ $reports->brief_des}}
</textarea>
</div>
<div class="form-group required">
<label>Full Description</label></br>
<textarea id="full_des" name="full_des" rows="12" cols="70" style="width: 80%">
{{ $reports->full_des}}
</textarea>
</div>
<input type="submit" value="Update"/>
</td>
<td>
</td>
</tr>
</table>
</div>
{{ Form::token() }}
</form>
#endsection
and Controller update method code is as:
public function update(Request $request, $id){
report::find($id)->update($request->all());
return redirect()->route('backend.index')
->with('success','Product Updated Successfully');
}
and route.php is as:
Route::resource('backend', 'ReportController');
when I am submitting the form then I am getting any update in my database and redirect is not working also.

How to perform arithmetic operations of two different data types in thymeleaf?

I have a piece of HTML where I need to multiply (or perform arithmetic operations) two values using thymeleaf.
Let's say my price is double and quantity is an integer.
Here's my sample code:
<fieldset class="form-inline" disabled >
<label for= "prodAmount">AMOUNT: </label>
<input type="number" class="form-control" th:text="${#aggregates.sum(product.![price * qty])}">
</fieldset>
Here's the error I'm getting:
Exception evaluating SpringEL expression: "#aggregates.sum(product.![price * qty])"
Also , I would also like to ask how to perform this when there's two or more values.
Here's my html form
<div class="container">
<div class="row">
<div class="col-lg-4 col-sm-12 col-xs-12">
<form th:action="#{/product/product-list}" method="post" th:object="${productEntity}">
<input type="hidden" th:field="*{pid}" th:value="${pid}" >
<fieldset class="form-group">
<label for="product_name">Product Name</label>
<input type="text" class="form-control" th:field="*{pName}" th:value="${pName}">
</fieldset>
<fieldset class="form-group">
<label for="price">Price</label>
<input type="number" class="form-control" th:field="*{price}" th:value="${price}" placeholder="0">
</fieldset>
<fieldset class="form-group">
<label for="pQty">Quantity</label>
<input type="number" class="form-control" th:field="*{qty}" th:value="${qty}" placeholder = "0">
</fieldset>
<fieldset class="form-group">
<label for="status">Status</label>
<select class="form-control" id="select_status" th:field="*{status}" th:value="${status}">
<option value="0">AVAILABLE</option>
<option value ="1">NOT AVAILABLE</option>
</select>
</fieldset>
<fieldset class="form-group">
<label for="image">Image</label>
<input id="input-b2" name="input-b2" type="file" class="file"
data-show-preview="false" th:field="*{image}" th:value="${image}">
</fieldset>
</form>
</div>
<div class="col-lg-8 col-sm-12 col-xs-12">
<table class="table table-inverse table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Status</th>
<th>Image</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr th:each="product : ${productTable} ">
<td th:text="${product.pid}"></td>
<td th:text="${product.pName}"></td>
<td th:text="${product.price}"></td>
<td th:text="${product.qty}"></td>
<td th:text="${product.status == '0'} ? 'Available' : 'Not Available'"></td>
<td th:text="${product.image}"></td>
<td>
<a th:href="#{/productlist/{id}(id = ${product.pid})}" class="btn btn-primary">Update</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="row float-right">
<div class="col-xd-12">
<div>
<fieldset class="form-inline" disabled >
<label for= "prodAmount">AMOUNT: </label>
<input type="number" class="form-control" th:text="${#aggregates.sum(product.{price * qty})}">
</fieldset>
<button type="button" class="btn btn-primary">Checkout</button>
</div>
</div>
</div>
</div>
Product Controller:
#RequestMapping(value="/product-list")
public ModelAndView shopView() {
ModelAndView mav = new ModelAndView();
mav.addObject("productTable", pRepo.findAll());
mav.addObject("productEntity",new ProductEntity());
mav.setViewName("/productlist");
return mav;
}
#RequestMapping(value = "/{id}",method = RequestMethod.GET)
public ModelAndView getProduct(#ModelAttribute ProductEntity productEntity, #PathVariable int id) {
ModelAndView mav = new ModelAndView();
mav.addObject("productTable",pRepo.findAll());
mav.addObject("productEntity", pRepo.findOne(id));
mav.setViewName("/productlist");
return mav;
}

Resources