How to pass data to a form in laravel 8 - laravel

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

Related

How to show a new column after using select2 in laravel?

I have a form and using select2 for filter. I want to show new column by using this select2.
I don't know its possible or not but i want to do this . using jquery or something, can someone reference to me about this? its my form for my input
<form class="form-prevent-multiple-submits" data-toggle="validator" action="{{route('update_sarpras_aduan'),'test'}} " method="post">
#csrf
{{method_field('patch')}}
<div class="form-group">
<label class="control-label" for="title">Nama Unit:</label>
<input type="text" name="nama_unit" id="nama_unit" class="form-control" data-error="Please enter title." required disabled/>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label class="control-label" for="title">Tanggal Aduan:</label>
<textarea name="created_at" class="form-control" id="tgl_aduan" data-error="Please enter description." readonly ></textarea>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label class="control-label" for="title">Nama Pengadu:</label>
<input type="text" name="nama_pengadu" class="form-control" id="nama_pengadu" data-error="Please enter title." readonly />
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label> Isi Aduan </label>
<textarea name="aduan" id="aduan" class="form-control" rows="3" placeholder="Isi Aduan atau Keluhan ..." readonly ></textarea>
</div>
<div class="form-group">
<label class="control-label" for="title">Nama Teknisi:</label>
<input type="text" name="nama_teknisi" class="form-control" id="nama_teknisi" data-error="Please enter title." readonly />
<div class="help-block with-errors"></div>
</div>
----------------here this select2---------------------------
<div class="form-group">
<label>Pilih Inventaris</label>
<select class="form-control select2bs4" name="alat_id" id="alat_id" style="width: 100%;" aria-hidden="true">
#foreach($inven as $id => $item )
<option value="{{ $id }}">{{ $item }} </option>
#endforeach
</select>
</div>
--------------------------------------------------
<div class="form-group">
<label>Status Saat Ini (Jangan Lupa Diganti)</label>
<select name="status" id="status" class="form-control">
<option value="Tindakan Lanjutan">Tindakan Lanjutan</option>
<option value="Selesai">Selesai</option>
</select>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label> Tindakan Pelaksana </label>
<textarea name="tindakan" id="tindakan" class="form-control" rows="3" placeholder="Isi Tindakan Yang dilakukan" required ></textarea>
</div>
<div class="form-group">
<label>Kondisi Alat </label>
<select name="status_alat" id="status_alat" class="form-control">
<option value="Baik">Baik</option>
<option value="Rusak Ringan">Rusak Ringan</option>
<option value="Rusak Berat">Rusak Berat</option>
</select>
<div class="help-block with-errors"></div>
</div>
<input type="hidden" name="id" id="id" value="">
<input type="hidden" name="user_id" id="user_id" value="">
<input type="hidden" name="ipsrs_id" id="ipsrs_id" value="">
<input type="hidden" name="teknisi_id" id="teknisi_id" value="">
<input type="hidden" name="ended_at" id="ended_at" value="">
<div class="form-group">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<button type="submit" id="button" class="btn btn-success crud-submit-edit button-prevent-multiple-submits">
<i class="spinner fa fa-spinner fa-spin" ></i>
Submit</button>
</div>
</form>
after using this select2 , will showing a new form . can someone references to me about this ?
//select input
<div class="form-group">
<label>Pilih Inventaris</label>
<select class="form-control select2bs4" name="alat_id" id="alat_id" style="width: 100%;" aria-hidden="true" onchange="Show()">
#foreach($inven as $id => $item )
<option value="{{ $id }}">{{ $item }} </option>
#endforeach
</select>
</div>
//new form
<div id="divid" style="display:none">
<form method="" action="">
#csrf
</form
<div>
//javascript code
<script>
function Show()
{
var fieldValue = $('#alat_id').val();
if(fieldValue == "")
{
document.getElementById("divid").style.display = 'none';
}
else{
document.getElementById("divid").style.display = 'inline'
}
}
</script>

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');

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.

Laravel Dynamic Form Submit not responding

I have a controller ( Laravel ) which will be dynamic create a form as below
<div class="row" style="margin-bottom:30px;margin-top:20px;">
<div class="col-2">
<form class="addform" id="newcheckform" role="form">
'.csrf_field().'
<label class="" for="cust_code">Cust Code</label>
<input type="text" class="form-control" id="cust_code" value="'.$row->cust_no.'">
</div>
<div class="col-2">
<label class="" for="cust_short">Cust Shortname</label>
<input type="text" class="form-control" id="cust_short">
</div>
<div class="col-4">
<label class="" for="cust_name">Cust Name</label>
<input type="text" class="form-control" id="cust_name" value="'.$row->cust.'">
</div>
<div class="col-2">
<label class="" for="region">Region</label>
<input type="text" class="form-control" id="region">
</div>
<div class="col-2">
<label class="" for="region">Add Cust Data</label>
<button class="btn btn-sm btn-info" id="createnewcust" type="submit">Create</button>
</form>
</div>
</div>
if will be loaded by below script in the blade file
$("#custcheck").load("{{ url('/sales/admin/custcheck') }}");
The form displayed as its should to be.
However I when I submit button. Nothing happened. code as below. Appreciate anyone can help. this script resides in the same blade file.
$(function() {
$(document).on('submit', '#newcheckform', function(e){
alert("Hi");
});
});
You have wrong closing tags for <form> change something like this:
<div class="row" style="margin-bottom:30px;margin-top:20px;">
<form class="addform" id="newcheckform" role="form">
{{csrf_field()}}
<div class="col-2">
<label class="" for="cust_code">Cust Code</label>
<input type="text" class="form-control" id="cust_code" value="{{$row->cust_no}}">
</div>
<div class="col-2">
<label class="" for="cust_short">Cust Shortname</label>
<input type="text" class="form-control" id="cust_short">
</div>
<div class="col-4">
<label class="" for="cust_name">Cust Name</label>
<input type="text" class="form-control" id="cust_name" value="{{$row->cust}}">
</div>
<div class="col-2">
<label class="" for="region">Region</label>
<input type="text" class="form-control" id="region">
</div>
<div class="col-2">
<label class="" for="region">Add Cust Data</label>
<button class="btn btn-sm btn-info" id="createnewcust" type="submit">Create</button>
</div>
</form>
</div>
This will work.

how to save data/records from html form to DB2 database

this is my html form
<form class="form-horizontal" id="myCredantials" >
<div class="form-group">
<label class="col-sm-2 control-label">Username : </label>
<div class="col-sm-10 col-md-2">
<input type="text" class="form-control" id="tbUsername" placeholder="Username">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Email : </label>
<div class="col-sm-10 col-md-2">
<input type="text" class="form-control" id="tbEmail" placeholder="Email">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Gender : </label>
<div class="col-sm-10 col-md-2">
<select id="cbmGender">
<option value="-1">Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Date : </label>
<div class="col-sm-10 col-md-2">
<input type="text" class="form-control" id="tbDate">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Price : </label>
<div class="col-sm-10 col-md-2">
<input type="text" class="form-control" value="0" id="tbPrice" placeholder="Prices">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Perc % : </label>
<div class="col-sm-10 col-md-2">
<input type="text" class="form-control" value="0" id="tbPercentage" placeholder="%">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Password : </label>
<div class="col-sm-10 col-md-2">
<input type="password" class="form-control" id="tbPassword" placeholder="Password">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Confirm Password : </label>
<div class="col-sm-10 col-md-2">
<input type="password" class="form-control" id="tbConfirmpassword" placeholder="Confirm">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="button" class="btn btn-primary " id="btnSubmit" onclick="onSave()" value="Save" >
<input type="button" class="btn btn-primary " id="btnClear" onclick="onClearError()" value="Remove Error" >
<input type="button" class="btn btn-primary " id="btnGet" onclick="doGet()" value="Get" >
<input type="button" class="btn btn-primary " id="btnGet" onclick="doPost()" value="Post" >
</div>
</div>
<div style="width:700px;">
<table cellpadding="0" cellspacing="0" border="0" width="700" id="tbl_Credantials">
</table>
</div>
</form>

Resources