variable showing null value in view laravel - laravel

i am trying to sent data to view from function in controller but the variable view is not showing any result
public function goedit($id)
{
$catg = Category::where('cat_id',$id)->first();
return view('product.gocatedit')->with('row', $catg);
}
and my view is
#if(isset($row))
<form action="{{action('ProductController#edit')}}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="col-sm-12">
<h1 style="text-align:center;">Edit Items</h1>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Category</th>
<th>Item</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<input type="hidden" name="item_id" value="{{ $row->item_id}}">
<td><input class="form-control" name="item_name" value="{{$row->item_name}}" /></td>
<td><input class="form-control" name="item_price" value="{{$row->item_price}}" /></td>
<td><input class="btn btn-primary btn-block btn-flat" type="submit" value="Edit"></td>
</tr>
</tbody>
</table>
</div>
</form>
#endif
please help thanks

It seems you query returning null, that's the problem (there is no such ID in a database).
If you want to check for null, use is_null:
#if(is_null($var))
Or maybe you want to use empty:
#if(!empty($row))

public function goedit($id)
{
$catg = Category::where('cat_id',$id)->first();
return view('product.gocatedit')->with('row', $catg);
}
practically should be:
public function goedit($id)
{
$catg = Category::findOrFail($id);
return view('product.gocatedit')->with('row', $catg);
}
and then it seems that you aren't using default primary key, which is "id", so in your model add
public class Category{
...................
protected $primaryKey = "cat_id";
.....................
}

If you use ->with('something', 'data'), then the data passed will be in the session('something') and it's purpose is to 'flash' messages.
You need this (view data is supposed to be passed as an array, and as a second parameter of the view() function):
public function goedit($id)
{
$catg = Category::where('cat_id',$id)->first();
return view('product.gocatedit', ['row' => $catg]);
}

Related

How do i sent dynamic values from form (post method) back to controller

I am quite new to laravel,
Basically, I have this Form where I am showing values Dynamically, and those values i need to pass into controller again for Delete, but not understanding how to do it.
my blade is as bellow
<div class="col-md-12">
#if(isset($rtn_user_id))
#csrf
<table>
<tr>
<td>Customer Name</td> <td>Vechile Name</td> <td>Imei</td><td>Actions</td>
</tr>
<tr>
<td> {{$rtn_user_id}}</td> <td></td> <td> {{$rtn_device_name}}</td> <td>{{$rtn_imei}}</td> <td><button type="button">Delete</button></td>
</tr>
</table>
#endif
</div>
</form>
my operation.delete_imei is like this
so I want to pass the values of $rtn_imei to the controller
public function delete_imei(Request $request)
{
$post_imei = $request->imei;
dd($pos_imei)
}
web.php
Route::post('deleteimei', 'Operation#delete_imei')->name('operation.delete_imei');
Can you please help me with this
You need to issue an HTTP post to /deleteimei, which is really an HTML/JS question, depending on how you choose to do it.
Assuming you're not using a JS library which makes life easier, this is how you'll want to do it (change the selectors etc. to suit):
document.getElementsByTagName('button').click = function() {
xhttp.open('POST', '/deleteimei', true);
xhttp.send();
location.reload();
}
In Blade File
#csrf
#if(isset($rtn_user_id))
<tr>
<td>Customer Name</td> <td>Vechile Name</td> <td>Imei</td><td>Actions</td>
</tr>
<tr>
<td> {{$rtn_user_id}}</td> <td></td> <td> {{$rtn_device_name}}</td> <td>{{$rtn_imei}}</td> <td><button formaction="{{route('operation.delete_imei',$rtn_imei)}}" type="submit" class="btn btn-danger btn-sm"><i class="fa fa-trash-o" aria-hidden="true"></i></button></td>
</tr>
</table>
#endif
</div>
</form>
web.php
Route::delete('deleteimei/{id}', 'Operation#delete_imei')->name('operation.delete_imei');
-In Operation Controller
public function delete_imei($id) {
$post_imei = $id;
dd($pos_imei)
$data=DB::delete('delete from delete_imei where id=?',[$id]);
return redirect(route('index'));
}
i think you should edit your blade like this and this will send your form's data to your controller and you will be suitable for get your request's datas.
<form role="form" method="POST" action="{{ route('operation.delete_imei') }}">
#csrf
<div class="col-md-12">
#if(isset($rtn_user_id))
<table>
<tr>
<td>Customer Name</td> <td>Vechile Name</td> <td>Imei</td><td>Actions</td>
</tr>
<tr>
<td> {{$rtn_user_id}}</td> <td></td> <td> {{$rtn_device_name}}</td> <td>{{$rtn_imei}}</td> <td><button type="button">Delete</button></td>
</tr>
</table>
#endif
</div>
</form>
if you want to pass some data from blade to controller just put some hidden input in your form and set the value of that with your data you want to pass:
<input id="invisible_id" name="invisible" type="hidden" value="{{$data}}">

How to get Selected checkbox into database

I have a page for purchase order in that page i show data from table obat and i want to get selected checkbox using that data into table purchase order
This is my table obat
<table class="table table-bordered" style="margin-top:20px">
<tbody>
<tr class="text-center">
<th>No</th>
<td>Kode Obat</td>
<td>Nama Obat</td>
<td>Harga</td>
</tr>
#foreach ($obat as $key =>$o)
<tr>
<th class="text-center">
#foreach ($po as $po)
<input type="checkbox" name="select" id="select">
#endforeach
</th>
<td>
<input type="text" class="form-control" value="{{ $o->kode_obat }}">
</td>
<td>
<input type="text" class="form-control" value="{{ $o->nama_obat }}">
</td>
<td>
<input type="text" class="form-control" value="{{ $o->harga_obat }}">
</td>
</tr>
#endforeach
</tbody>
</table>
The checkbox(select) is from table purchase order but it can't show. if i did't use foreach its show
you can pass array also to check if it is in the array or not then you can this like below.
in your controller
public function edit($id)
{
$odat=Odat::all();
foreach($odat as $od)
{
$listod[]=$odat->select;
}
return view('yourbladefile',compact('roles','listod'));
}
then in your view
<input type="checkbox" name="select" #if (in_array(yourpurchaseordernamefromdb, $listod))
{{'checked'}} #endif>Selected</td>
personally i managed liked this. hope it helps

Undefined variable in action route with parameter for laravel

This is my view, and I want to pass r{{$i}}_selected to controller, but it gives me an error. Undefined variable: enterprise . How can I solve this?
<form name="frm-example" id="frm-example"
action="{{ route('pengawas_alokasi.store', $enterprise ) }}"
method="post">
{{ csrf_field() }}
<div class="table-responsive text-center">
<table class="table table-borderless table-striped table-hover" id="myTable">
<thead class="thead-dark">
<tr>
<th></th>
<th class="text-center">#</th>
<th class="text-center">Nama Perusahaan</th>
</tr>
</thead>
<?php $i=1;?> #foreach($enterprises as $enterprise)
<tr>
{{--
<td></td> --}}
<td>
<input type="checkbox" name="r{{$i}}_selected" value="{{$enterprise->id}}">
</td>
<td>{{$i}}</td>
{{-- <td>
<input type="hidden" name="nomor{{$i}}" value="{{$i}}" size="3">{{$i}}</td> --}}
<td>{{$enterprise->nama_perusahaan}}</td>
{{-- <td>
<input type="hidden" name="nama_perusahaan{{$i}}" value="{{$enterprise->nama_perusahaan}}" size="3">{{$enterprise->nama_perusahaan}}</td> --}}
</tr>
<?php $i=$i+1;?> #endforeach
</table>
</div>
<div class="form-group">
<input type="submit" name="" class="btn btn-primary" value="Submit">
</div>
</form>
This is the controller that responsible to the view above
public function insert($id)
{
$enterprises = Enterprise::get();
return view('pengawas.pengawas-insert-alokasi', compact('enterprises'));
}
This is the controller that should store the value that I want to retrieve in my view form
public function store_enterprise(Request $request){
//I'm not doing it yet
dd($request);
}
because you are sending "enterprises" from your controller and you are trying to send "enterprise" in your form's action
you can use hidden variable in your form instead of use URL variable :
<input type="hidden" name="enterprise" value="{{$enterprise}}">
and in your controller :
public function insert(Request $request)
{
//get enterprise value here
$enterprise = $request->enterprise;
}

Getting the selected values from a checkbox list to the controller with Spring Boot

I'm using Spring Boot with Thymeleaf as viewer, and I want to delete all selected items from a table. Therefore, I need to pass to the controller a list with the values from the selected checkboxes.
This is my approach for the controller:
#PostMapping("/admin/rates/prices/delete")
public String delete(#ModelAttribute Rate price, ServletWebRequest request){
if(request.getParameterValues("idChecked") != null){
for(String idCheckedStr : request.getParameterValues("idChecked")){
int idrate = Integer.getInteger(idCheckedStr);
rateRepository.deleteRate(idrate);
}
}
return "redirect:/admin/rates/prices";
}
I get a Null Pointer Exception:
java.lang.NullPointerException: null
at com.rentalwebs.controllers.rates.PriceListController.delete(PriceListController.java:42)
I think this method should collet the values::
request.getParameterValues("idChecked")
This is the line at the form, which creates each checkbox with the Thymeleaf annotations:
<input type="checkbox" th:name="idChecked" th:value="${price.idrate}"/>
That's the view code for the form:
<form th:action='#{/admin/rates/prices/delete}'
method="POST"
th:object="${rate}">
<button type="submit" name='delete' value="delete"
class='btn btn-secondary btn-sm'
th:text="#{delete}"
data-toggle="tooltip" data-placement="right"
th:title="#{delete.selected}">
</button>
<p></p>
<table class='table table-sm responsive'>
<thead class='thead-default'>
<tr>
<th><input type="checkbox" id="checkAll"/></th>
<th th:text='#{from}'></th>
<th th:text='#{to}'></th>
<th th:text='#{price}'></th>
</tr>
</thead>
<tbody>
<tr th:each="price : ${prices}">
<td>
<input type="checkbox" th:name="idChecked" th:value="${price.idrate}"/>
</td>
<td th:text="${#temporals.format(price.datefrom, 'dd/MM/yyyy')}"></td>
<td th:text="${#temporals.format(price.dateto, 'dd/MM/yyyy')}"></td>
<td th:text="${price.price} + ' €'"></td>
</tr>
</tbody>
</table>
</form>
Thank you in advance for your help :-)
This is the code with the solution, taken from the comments:
#PostMapping("/admin/rates/prices")
public String delete(#RequestParam("idChecked") List<String> idrates){
if(idrates != null){
for(String idrateStr : idrates){
int idrate = Integer.parseInt(idrateStr);
rateRepository.deleteRate(idrate);
}
}
return "redirect:/admin/rates/prices";
}

Values for th:field attributes in checkbox

I have table with datas from database (insert dynamically). In one column I insert checkbox. Now I want to select one of them and send to next form (I select one product and send properties to another form. In this form should be displayed properties only the select product). But I don't know what kind of value insert in th:field="*{}". I tried many solutions but doesn't work. My html form with all products table:
<form action="/oferta/zamow" th:action="#{/oferta/zamow}"
th:object="${oferta}" method="post">
<table border="1" id="display-data">
<tr>
<td>#</td>
<td>title</td>
<td>author</td>
<td>rok</td>
<td>cena</td>
<td></td>
</tr>
<tr th:each="produkt, pozycja : ${oferta}">
<td th:text="${pozycja.count}"></td>
<td><span th:text="${produkt.tytul}"></span></td>
<td><span th:text="${produkt.autor}"></span></td>
<td><span th:text="${produkt.rok}"></span></td>
<td><span th:text="${produkt.cena}"></span></td>
<td>
<input type="submit" value="zamow"/>
<!-- <a th:href="#{/zamowienie}">zamow</a> -->
</td>
<td>
<label>zamow</label>
<input type="checkbox" th:field="*{produkt}" th:value="${produkt}"/>
</td>
</tr>
</table>
</form>
Form to display select product:
<form action="/zamowienie/zam" th:action="#{/zamowienie/zam}"
th:object="${zamowienie}" method="post">
<table border="1" id="display-data">
<tr align="center">
<td colspan="2">twoje zamowienie</td>
</tr>
<tr>
<td>tytul</td>
<td><span th:text="${produkt.tytul}"></span></td>
</tr>
<tr>
<td>autor</td>
<td><span th:text="${produkt.autor}"></span></td>
</tr>
<tr>
<td>rok</td>
<td><span th:text="${produkt.rok}"></span></td>
</tr>
<tr>
<td>cena</td>
<td><span th:text="${produkt.cena}"></span></td>
</tr>
<tr>
<td>data zlozenia zamowienia</td>
<td><span th:text="${datazam}"></span></td>
</tr>
</table>
</form>
Thanks for help.
I am not sure if this is the answer you seek, but you can find an example at http://www.thymeleaf.org/doc/html/Thymeleaf-Spring3.html#checkbox-fields.
Here is a simple example to illustrate how to use a checkbox in Thymeleaf with Spring MVC.
Controller:
#RequestMapping(value = "/showForm", method=RequestMethod.GET)
public String showForm(Model model) {
List<String> allItems = new ArrayList<String>();
allItems.add("value1");
allItems.add("value2");
allItems.add("value3");
model.addAttribute("allItems", allItems);
Foo foo = new Foo();
List<String> checkedItems = new ArrayList<String>();
// value1 will be checked by default.
checkedItems.add("value1");
foo.setCheckedItems(checkedItems);
model.addAttribute("foo", foo);
...
}
#RequestMapping(value = "/processForm", method=RequestMethod.POST)
public String processForm(#ModelAttribute(value="foo") Foo foo) {
// Get value of checked item.
List<String> checkedItems = foo.getCheckedItems();
...
}
html:
<form action="#" th:action="#{/processForm}" th:object="${foo}" method="post">
<div th:each="item : ${allItems}">
<input type="checkbox" th:field="*{checkedItems}" th:value="${item}" />
<label th:text="${item}">example</label>
</div>
<input type="submit" />
</form>
Foo.java:
public class Foo {
private List<String> checkedItems;
public List<String> getCheckedItems() {
return checkedItems;
}
public void setCheckedItems(List<String> checkedItems) {
this.checkedItems = checkedItems;
}
}
Hope this helps.
Take a look at the thymeleaf spring integration docs.
All th:field are mapped against the command object. Thats why you need the *{} expression.
One thing the template engine is not able to do (yet) is mapping fields inside a loop directly. So you cannot use the *{} approach to reference the produkt variable from the loop.
What you have to do is use the index of the th:each expression and build a property accessor with a pre-evaluated expression for the index.
<input type="checkbox" th:field="*{produkts[__${index}__].checked" />
You do not need the th:value, th:field is taking care of it. (Except if you want to superseed it)

Resources