Refresh datatable after updating row using modal window - laravel

I'm using Jquery Datatable for a small project along with Laravel 5.6,
one of the datatable columns allows to edit a specific row , (by the way, I'm not using datatable editor plugin which has to be adquired).
the modal window contains the update form with the jquery logic, when i click update i don't see the updated row changes on datatable.
modal window to update datatable row
modal window code:
<div class="row padded">
<div class="col-md-12">
<div class="form-group">
</div>
{!! Form::open([ 'route' =>['srh.prestamo_pago.update', $prestamo_pago->id] ,'method' => 'PUT', 'id' => 'frmEdit_pago']) !!}
<div class="form-group">
{{ Form::label('Nro Cuota', 'Nro Cuota') }}
{{ Form::text('nro_cuota', $prestamo_pago->nro_cuota, array('class' =>'form-control', 'required' =>'', 'max-length' => '50', 'id' => 'nro_cuota')) }}
</div>
<div class="form-group">
{{ Form::label('Monto Cuota', 'Monto cuota') }}
{{ Form::text('monto_cuota', $prestamo_pago->monto_cuota, array('class' =>'form-control', 'required' =>'', 'max-length' => '50','id' => 'monto_cuota', "readonly" )) }}
</div>
<div class="form-group">
{{ Form::label('Fecha Pago', 'Fecha Pago') }}
#if(isset($prestamo_pago->fecha_pago))
{{ Form::text('fecha_pago', date("d/m/Y", strtotime($prestamo_pago->fecha_pago)) , array('class' =>'form-control datepicker1', 'required' =>'', 'max-length' => '50','id' => 'fecha_pago')) }}
#else
{{ Form::text('fecha_pago', null , array('class' =>'form-control datepicker1', 'required' =>'', 'max-length' => '50','id' => 'fecha_pago')) }}
#endif
</div>
<div class="form-group">
{{ Form::label('Fecha Vencimiento', 'Fecha Vencimiento') }}
{{ Form::text('fecha_vencimiento', date("d/m/Y", strtotime($prestamo_pago->fecha_vencimiento)), array('class' =>'form-control', 'required' =>'', 'max-length' => '50','id' => 'fecha_vencimiento', 'readonly'=>'')) }}
</div>
<div class="form-group">
{{ Form::label('Seleccione', 'Seleccione Estado') }}
{{ Form::select('estado_cuota', array( 'CANCELADO' =>'CANCELADO', 'PENDIENTE' =>'PENDIENTE'), $prestamo_pago->estado_cuota, array('class' =>'form-control', 'required' =>'', 'id' => 'estado_cuota'))}}
</div>
<div class="form-group">
{{ Form::label('Tipo Asiento', 'Tipo Asiento') }}
<input name="tipo_asiento" type="text" value="{{ $prestamo_pago->tipo_asiento_ingreso }}" id="tipo_asiento" class="form-control" readonly="readonly">
</div>
<div class="form-group">
{{ Form::label('Tipo Obligacion', 'Tipo Obligacion') }}
<input name="tipo_obligacion" type="text" value="{{$prestamo_pago->tipo_obligacion_ingreso}}" id="tipo_obligacion" class="form-control" readonly="readonly">
</div>
<div class="form-group">
{{ Form::label('Codigo Cuenta', 'Codigo Cuenta ') }}
{{ Form::text('cod_cuenta', $prestamo_pago->cod_cuenta, array('class' =>'form-control', 'required' =>'', 'max-length' => '50','id' => 'cod_cuenta','readonly'=>'')) }}
</div>
<div class="form-group">
{{ Form::label('Observación')}}
{{ Form::textarea('glosa', null,
array('class' => 'form-control',
'id' => 'glosa',
'size' => '30x3',
'required' => '',
'placeholder' => 'Ingrese Observación')) }}
</div>
<div class="form-group">
{{ Form::hidden('id_cuota', $prestamo_pago->id, array('id' => 'id_cuota')) }}
{{ Form::hidden('estado_final_cuotas','', array('id' => 'estado_final_cuotas')) }} // SI SE HAN CANCELADO TODAS LAS CUOTAS
{{ Form::hidden('prestamo_solicitud_id', $prestamo_pago->prestamo_solicitud_id, array('id' => 'prestamo_solicitud_id')) }}
{{ Form::hidden('nro_obligacion', '', array('id' => 'nro_obligacion')) }}
{{ Form::hidden('nro_asiento', '', array('id' => 'nro_asiento')) }}
{{ Form::hidden('contracuenta','', array('id' => 'contracuenta')) }}
{{ Form::submit('Actualizar Pago', array('class' => 'btn btn-primary btn-block', 'style' => 'margin-top:20px')) }}
</div>
{!! Form::close() !!}
</div>
</div>
<script type="text/javascript">
$(function(){
// FORM DATA AND HIDDEN FORM FIELDS
$('#frmEdit_pago').on('submit', function(e){
e.preventDefault();
var id = $('#id_cuota').val();
var tipo_asiento = $('#tipo_asiento').val();
var tipo_obligacion = $('#tipo_obligacion').val();
var cuenta_ingreso = $('#cod_cuenta').val();
var nro_cuota = $('#nro_cuota').val();
var prestamo_solicitud_id = $('#prestamo_solicitud_id').val();
var estado_cuota = $('#estado_cuota').val();
//pass search params and get data from database , then load hidden form fields
$.get('prestamo_pago/get_datos_contables',
{
'tipo_obligacion':tipo_obligacion,
'tipo_asiento': tipo_asiento,
'cuenta_ingreso' : cuenta_ingreso,
'nro_cuota' : nro_cuota,
'prestamo_solicitud_id' : prestamo_solicitud_id,
},
function(data) {
console.log(data);
$('#nro_obligacion').val(data.nro_obligacion);
$('#nro_asiento').val(data.nro_asiento);
$('#contracuenta').val(data.contracuenta.contracuenta_gasto);
var frm = $('#frmEdit_pago')[0];
var send = $(frm).serializeArray();
$.post(frm.action, send,
function(data) {
console.log(data);
//*****************************************************************
Here i want to refresh datatable to display updated row, I get datatable1.rows.data is not a function error, i think it doesn't get the instance of the main window.
datatable1.clear().draw();
datatable1.rows.data(data.cuotas_prestamo).draw();
//******************************************************************
toastr.success('Se ha guardado la información');
$('#modal-pago').modal('hide');
},'json'
).fail(funFail);
},'json'
).fail(funFail);
});
});
PS.- the fact that I'm getting submit code in the modal window and not on the index.php that holds datatatable, is because I put hidden fields in the form and it has to be displayed for that to happen. maybe there's a way to pass datatable instance to the modal ?

Related

Laravel form model attributes

How to pass attributes like id, class to form model?
This what i try not working and in official Laravel documentation wass not defined.
#if(isset($country))
{{ Form::model($country, ['route' => ['country.show', $country->id]], ['class' => "123"]) }}
#else
{{ Form::open(['route' => 'country.store', 'id'=> 'admin_store_form' ]) }}
#endif
<div class="row mb-2">
<div class="col-sm-1">
{{ Form::label('name', 'Name', ['class' => 'col-sm-2 admin-label col-form-label']) }}
</div>
<div class="col-sm-3">
{{ Form::text('name', old('name'), ['placeholder'=>'Name', 'class' => 'form-control form-control-sm admin-control']) }}
</div>
</div>
{{ Form::close() }}
I define 'class' => "123" but that not work.
The correct way to add a class to Form::model is:
{{ Form::model($country, ['route' => ['country.show', $country->id], 'class' => '123']) }}
You need to add the class key to the same array argument as route.

Laravel 8.0 Error "Action App\Http\Controllers\SkladController not defined."

This is my problem in SkladController. I want to fetch data from the DB but it does show me anything because I have that error in the picture.
SkladController
class SkladController extends Controller
{
public function index()
{
$sklads = Sklad::all();
return view('sklads.index')->with('sklads', $sklads);
}
public function create()
{
return view('sklads.index');
}
public function store(Request $request)
{
$this->validate($request, [
'datle' => 'required',
'mandle' => 'required',
'marcipan' => 'required',
'orechy' => 'required',
]);
//vytvorit v sklade
$sklads = new Sklad;
$sklads->datle = $request->input('datle');
$sklads->mandle = $request->input('mandle');
$sklads->marcipan = $request->input('marcipan');
$sklads->orechy = $request->input('orechy');
$sklads->save();
return redirect('/sklad')->with('success', 'Uložené');
}
}
index.blade.php
<h3> Príjem v sklade</h3>
<br>
{!! Form::open(['action' => 'App\Http\Controllers\SkladController', 'method' => 'POST']) !!}
<div class="row">
<div class="form-group col-md-2">
{{ Form::number('datle', '', ['class' => 'form-control', 'placeholder' => 'Ďatle']) }}
</div>
<div class="form-group col-md-2">
{{ Form::number('mandle', '', ['class' => 'form-control', 'placeholder' => 'Mandle']) }}
</div>
<div class="form-group col-md-2">
{{ Form::number('marcipan', '', ['class' => 'form-control', 'placeholder' => 'Marcipán']) }}
</div>
<div class="form-group col-md-2">
{{ Form::number('orechy', '', ['class' => 'form-control', 'placeholder' => 'Orechy']) }}
</div>
<div class="form-group col-md-2">
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
</div>
</div>
{!! Form::close() !!}
#if(count($sklads) > 0)
#foreach($sklads as $sklad)
<br>
{{$sklad->datle}} {{$sklad->mandle}} {{$sklad->marcipan}} {{$sklad->orechy}}
#endforeach
#else
<p>nenasli sa zaznamy</p>
#endif
web.php
Route::resource('/sklad', App\Http\Controllers\SkladController::class)
->except(['create', 'store', 'update', 'destroy']);
The issue is the action in
{!! Form::open(['action' => 'App\Http\Controllers\SkladController', 'method' => 'POST']) !!}
It should be
'action' => 'App\Http\Controllers\SkladController#index'
Usually you'd use the store method but you only don't have it according to your route declaration.
or you could use route instead.
{!! Form::open(['route' => 'sklads.index', 'method' => 'POST']) !!}

htmlentities error when passing from a modal

New to Laravel, please bare with.
Error:
htmlentities() expects parameter 1 to be string, object given (View: /var/www/html/willow/resources/views/emails/valuation.blade.php)
The modal from which it is being sent:
{!! Form::open(['action' => ['EnquiryController#valuationRequest']]) !!}
<div class="form-group">
{!! Form::text('name', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Name']) !!}
</div>
<div class="form-group">
{!! Form::text('email', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Email Address']) !!}
</div>
<div class="form-group">
{!! Form::text('telephone', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Telephone Number']) !!}
</div>
<div class="form-group">
{!! Form::text('house_number', null, ['class' => 'form-control has-feedback', 'placeholder' => 'House name / number']) !!}
</div>
<div class="form-group">
{!! Form::text('postcode', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Postcode']) !!}
</div>
<div class="form-group">
{!! Form::textarea('message', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Message', 'rows' => '5']) !!}
</div>
<div class="form-group">
<input type="submit" class="button black" value="Register">
</div>
{!! Form::close() !!}
and the function:
public function valuationRequest(ValuationRequest $request)
{
// dd($request->all());
Mail::send('emails.valuation',
['name' => $request['name'],
'email' => $request['email'],
'telephone' => $request['telephone'],
'house_number' => $request['house_number'],
'postcode' => $request['postcode'],
'message' => $request['message'],
],
function ($message) use ($request) {
$message->to('paolo#bigg.co.uk', 'Paolo Resteghini')->subject('Valuation Request - Willow Lettings');
});
Session::flash('flash_message', 'Your request has been sent.');
return redirect(URL::previous());
}
The contents of the DD are perfect. All of the requests are populated as expected, but when trying to go through the rest of the function it fails with the error above.
emails.valuation:
Hello, <br><br>
You have received a new valuation request via the Willow Lettings website. Here they are: <br><br>
<b>Name:</b> {{ $name }}<br>
<b>Email:</b> {{ $email }}<br>
<b>Phone:</b> {{ $telephone }}<br>
<b>House number:</b> {{ $house_number }}<br><br>
<b>Postcode:</b> {{ $postcode }}<br><br>
{{ $message }}
Most likely, this is a problem with your message variable. As you can see from the docs:
Note: A $message variable is always passed to e-mail views, and allows the inline embedding of attachments. So, you should avoid passing a message variable in your view payload.
In other words, you should change message into something else like msg.
'msg' => $request['message'],
Then, in your blade file, reflect that change:
{{ $msg }}

What am I doing wrong with my Form Model Binding?

I have method in a controller returning Employer based an id which it does...
public function talentProfile()
{
$user = Auth::user()->CandidateID;
$test_user = User::find($user);
foreach( $test_user->workHistory as $history ) {
echo $history;
}
return View::make('account.TalentProfile')->with( [ 'history' => $history, 'user' => $test_user] );
}
In my view I have a form I am trying to bind these result to. Looks like this....
#foreach( $history as $workHistory )
{{ Form::model($workHistory, array('route' => 'work-history-create', 'class' => 'form-horizontal work-history', 'files' => true) ) }}
<!-- Text input-->
<div class="form-group">
{{ Form::label('CompanyName', 'Company Name', array('class' => 'control-label') ) }}
{{ Form::text('CandidateID', null, array('class' => 'form-control', 'placeholder' => 'Company Name', 'title' => 'Company Name', 'required') ) }}
</div>
<!-- Text input-->
<div class="form-group">
{{ Form::label('JobTitle', 'Job Title', array('class' => 'control-label') ) }}
{{ Form::text('JobTitle', null, array('class' => 'form-control', 'placeholder' => 'Job Title', 'title' => 'JobTitle', 'required') ) }}
</div>
<!-- Text input-->
<div class="form-group">
{{ Form::label('Duties', 'Duties', array('class' => 'control-label') ) }}
{{ Form::text('Duties', null, array('class' => 'form-control', 'placeholder' => 'Duties', 'title' => 'Duties', 'required') ) }}
</div>
<div class="form-group">
{{ Form::label('Salary', 'Salary', array('class' => 'control-label') ) }}
{{ Form::number('Salary', null, array('class' => 'form-control', 'placeholder' => 'Salary', 'title' => 'Salary', 'required') ) }}
</div>
<div class="form-group">
{{ Form::label('NumDirectReports', 'Lackies', array('class' => 'control-label') ) }}
{{ Form::number('NumDirectReports', null, array('class' => 'form-control', 'placeholder' => 'Lackies', 'title' => 'Lackies', 'required') ) }}
</div>
<!-- Month/Year select -->
<div class="control-group">
{{ Form::label('EmploymentStart', 'Start Date', array('class' => 'control-label') ) }}
<div class="span3">
{{ Form::selectMonth('EmploymentStartMonth', null, array('class' => 'col-md-13', 'title' => 'employmentStartMonth') ) }}
</div>
<div class="span2">
{{ Form::selectYear('EmploymentStartYear', 1980, 2055, null, array('class' => 'col-lg-3', 'title' => 'employmentStartYear') ) }}
</div>
<!-- Month/Year select -->
<div id="fromPosition">
{{ Form::label('EmploymentEnd', 'End Date', array('class' => 'control-label') ) }}
<div class="span3">
{{ Form::selectMonth('EmploymentEndMonth', null, array('class' => 'col-md-13', 'title' => 'employmentEndMonth') ) }}
</div>
<div class="span2">
{{ Form::selectYear('EmploymentEndYear', 1980, 2055, null, array('class' => 'col-lg-3', 'title' => 'employmentEndYear') ) }}
</div>
</div>
<div class="span2">
{{ Form::checkbox('current-position', null, false, array('id' => 'current-position') ) }} This is my current position
</div>
</div>
#endforeach
My problem is the form is empty. I can dump $history right before the form and see my results. I don't know how to get those results into my form. In this scenario there are two results in $history so I need to have two separate forms displayed and populated with the binded data.
Hope this makes sense. Would love some help here. I'm struggling to understand why this doesn't work.
Thank you,
Your example is missing {{ Form::close() }}. Could it be that simple?
EDIT:
You are opening a new form with each loop in your example, but you never close any of them, as far as I can tell. If you only mean to open one form for the whole thing, you will not be able to use model binding, since you can only bind one model per form that way.

displaying specific error messages in laravel 4.2

hello i have this form and i have validations for it. I have already finished doing the validations for in inside my controller and i can already display the error messages in my view but i wanted the error message to be beside the input area where it came from
here is my code in the view
{{ Form::open(array('url' => 'addParentAccnt')) }}
<div class="form-group">
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username'), array('class' => 'form-control','placeholder' => 'Insert username')) }}
</div>
<div class="form-group">
{{ Form::label('fName', 'First Name') }}
{{ Form::text('fName', Input::old('fName'), array('class' => 'form-control','placeholder' => 'Insert First Name')) }}
</div>
<div class="form-group">
{{ Form::label('lName', 'Last Name') }}
{{ Form::text('lName', Input::old('lName'), array('class' => 'form-control','placeholder' => 'Insert Last Name')) }}
</div> {{ Form::submit('Proceed to Next Step', array('class' => 'btn btn-primary')) }}
{{ Form::close()}}
in the bottom of my view i added this code to display the error messages
#if ($errors->any())
<ul>
{{ implode('', $errors->all('<p style="color:red" class="error">:message</p>')) }}
</ul>
#endif
the code inside my controller is this
$rules = array
(
'username' => 'required|min:10|max:50',
'fName' => 'required|alpha|min:1|max:80',
'lName' => 'required|alpha|min:1|max:80',
);
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails())
{
return Redirect::to('createPa')
->withErrors($validator)
->withInput(Input::except('password'));
}
else
{
//do something
}
Change your view as following:
<div class="form-group">
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username'), array('class' => 'form-control','placeholder' => 'Insert username')) }}
{{ $errors->first('username', ':message') }}
</div>
<div class="form-group">
{{ Form::label('fName', 'First Name') }}
{{ Form::text('fName', Input::old('fName'), array('class' => 'form-control','placeholder' => 'Insert First Name')) }}
{{ $errors->first('fName', ':message') }}
</div>

Resources