I'm trying to update an existing row in my table using a form in a bootstrap modal. But nothing changes when I attempt it. I have an html table with a list of cars and in each row a button "Change Status" that opens the bootstrap modal with the id="myModal_{{ $car->id }}" and with the form inside it to edit the status of a car in repair.
Here's my modal:
<!-- Modal -->
<div class="modal fade" id="myModal_{{ $car->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Breyta stöðu bíls</h4>
</div>
<div class="modal-body">
<div class="form-group">
<h3>Breyta stöðu fyrir {{ $car->LicencePlate }}</h3>
</div>
{!! Form::open(['url' => 'create']) !!}
<div class="form-group">
{!! Form::label('Status', 'Status: ') !!}
{!! Form::select('Status', [
null => ' -- Veldu Stöðu bíls -- ',
'Í Biðröð' => 'Í Biðröð',
'Í Viðgerð' => 'Í Viðgerð',
'Tilbúinn' => 'Tilbúinn'
], null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
{!! Form::submit('Skrá stöðu', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
Update method in CarController:
public function CarEdit($id, Requests\CreateCarsRequest $request)
{
$edit = Car::update($request->all());
return redirect::back();
}
My Route to that method:
Route::post('/dashboard', 'CarController#CarEdit');
You should do this:
['action' => 'CarController#CarEdit']
Also, you're using mass assignment, so you must fill $fillable array in your Car model to make it work:
https://laravel.com/docs/5.1/eloquent#mass-assignment
I was able to fix this with the help of my colleagues. What I did was I created a new requet called UpdateCarRequest where the LicencePlate field was not unique. And edited my method like so:
public function CarEdit(Requests\UpdateCarRequest $request)
{
$edit = Car::where('LicencePlate', '=', $request->LicencePlate)->first();
$edit->update($request->all());
return redirect('/dashboard');
}
I also added a hidden field in the form with the value $car->LicencePlate.
Related
I want to create modal for update one of the column value from bootstrap table. here is the html code for opening modal.
<a href='' id='upload_link' data-id='$row->id' data-toggle='modal' data-target='#myModal'>Upload</a>
This is My Modal Code. In modal I have form and i want to pass data-id value in form action with route {!! Form::open(['route' => ['path_here',data-id] , 'files' => 'true','method' => 'get']) !!} so that i can update value of that particular record. How to pass data-id value in form action. How to do so ! please Help..
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Title</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
{!! Form::open(['route' => ['path_here',data-id] , 'files' => 'true','method' => 'get']) !!}
{!! Form::close() !!}
</div>
<!-- <div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div> -->
</div>
</div>
</div>
i'm using an AdminLTE package with Laravel to give me the AdminLTE theme for my app.
I cannot get the modal to launch at all. If I copy the contents of my form.blade.php into my index.blade.php it works. Although the way i have coded my controller & views i need them to be independent files.
Index.blade.php <- view serving the datatable
form.blade.php <-- view serving the modal from click edit on index.blade.php
CostCenterController.php <-- Controller responsible for both views.
CostCenterController.php
So in my controller I have a function that returns the results with server side processing into my datatable, this is just the part showing how each item in the table is generated, specifically with the btn-group for the edit function.:
$data = array();
if (!empty($costcenters)){
foreach ($costcenters as $costcenter){
$editURL = "delete/".$costcenter->id;
$temp['name'] = $costcenter->name;
$temp['code'] = $costcenter->code;
$action = '';
$action .= '
<div class="btn-group">
<i class="fa fa-edit"></i>
<i class="fa fa-trash"></i>
</div>
';
$temp['action'] = $action;
array_push($data, $temp);
}//end for each
}// end if empty
$recordsTotal = count($recordsTotal);
echo json_encode(array('recordsTotal' => $recordsTotal, 'data' => $data, 'recordsFiltered' => $recordsTotal));
In my index.blade.php i have nothing of importance or relevance really.
form.blade.php
I created a form.blade.php so when I hit edit, a route calls this view with the data needed from the edit function in the associated controller. This view works as plain html if opened in a new tab.
<div class="modal fade" id="modal" role="dialog">
<div class="modal-dialog" >
#if ($mode == 'edit')
{!! Form::model($costcenter,array('route'=>array('costcenter.update',$costcenter->id),'id'=>'costcenter-form')) !!}
#else
{!! Form::open(array('url'=>'costcenter/store','method'=>'post','data-toggle'=>'validator','id'=>'costcenter-form')) !!}
#endif
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">
#if ($mode == 'edit')
Edit {{$costcenter->name}}'s Cost Center
#else
Create Cost Center
#endif
</h5>
<button aria-label="Close" class="close" data-dismiss="modal" type="button"><span aria-hidden="true"> ×</span></button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
{!! Form::label('name','Cost Center Name:',['class'=>'control-label']) !!}
{!! Form::text('name',null,['class'=>'form-control','placeholder'=>'Enter Cost Center Name','data-error'=>"Please enter the name of the cost center",'required'=>true]) !!}
</div>
<div class="form-group">
{!! Form::label('code','CostCenter Code:',['class'=>'control-label']) !!}
{!! Form::text('code',null,['class'=>'form-control','placeholder'=>'Cost Center code as it appears in SYSPRO.','data-error'=>"Invalid Cost Center Code.",'required'=>true]) !!}
</div>
</form>
</div>
<div class="modal-footer">
{!! Form::submit('Save',['class'=>'btn btn-primary'])!!}
</div>
</div>
</div>
</div>
Any help would be appreciated.
I have a delete button beside my projects that shows a popup modal for confirmation whenever the button is clicked on. I want the yes button in the modal to then delete the project whenever this button is clicked on.
I'm calling the modal into the projects/show view with #include. The modal does appear when the button is clicked using the following jquery but the yes button when pressed is not deleting the project.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(".delete").on("submit", function(e){
e.preventDefault();
$('#myModal').modal('show');
});
});
The delete button beside projects:
{!! Form::open(['route' => ['projects.destroy', $projects->id], 'class' => 'delete', 'method' => 'DELETE']) !!}
{!!Form::button('<span class="glyphicon glyphicon-remove"></span>', ['class' => 'btn btn-danger btn-md', 'type' => 'submit'])!!}
{!! Form::close() !!}
Modal code:
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title text-center" id="myModalLabel">Delete Confirmation</h4>
</div>
<div class="modal-body text-center">
Are you sure you want to delete this project?
</div>
<div class="modal-footer">
<button type="submit" id="delete-btn" class="btn btn-default" >Yes</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
ProjectController:
<?php
namespace App\Http\Controllers;
use App\project;
use App\Http\Requests;
use Illuminate\Http\Request;
use Session;
class ProjectsController extends Controller
{
public function destroy($id){
$project = Project::find($id);
$project->delete();
Session::flash('success', 'The project was successfully deleted!');
return redirect()->route('projects.show', $project->project_id);
}
}
Change the submit button in your modal to this:
{!! Form::open(['route' => ['projects.destroy', $projects->id], 'class' => 'delete', 'method' => 'DELETE']) !!}
<button type="submit" id="delete-btn" class="btn btn-default" >Yes</button>
{!! Form::close() !!}
The best solution is to remove your implementation of the delete button you have now and make it a normal button. Let that button pop up the modal and replace your "Yes" button with an actual remove button like I did above.
I am doing project in laravel. I have multiple records in databse which I have displayed in laravel's blade file using foreach loop. Each record have pending button. Whenever user clicks on pending button it opens a html modal. I want that record id inside the modal but it always take first record id. My blade file looks like,
#foreach($providerData as $newprovider)
<h4>Name:{{$newprovider->name}}</h4>
<button class="btn btn-default" data-toggle="modal" data-target="#Pendingnote">Pending</button>
{!! Form::model( $newprovider->id ,['method' => 'PATCH','action'=>['superadminController#pendingProvider', $newprovider->id]]) !!}
<div class="modal fade" id="Pendingnote" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Note {!! $newprovider->id !!}</h4>
</div>
<div class="modal-body">
<input type="text" class="form-control" name="note" value="{{ old('note') }}">
</div>
<div class="modal-footer">
{!! Form::submit('Add', ['class' => 'btn btn-default']) !!}
</div>
</div>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</div>
{!! Form::close() !!}
#endforeach
Whichever record is clicked, it always returns $newprovider->id as first record's id. I don't know where I am getting wrong. Please help and thanks in advance.
Do these things in order to get the modals work.
Edit the button to:
<button class="btn btn-default" data-toggle="modal" data-target="#Pendingnote{{$newprovider->id}}">Pending</button>
And the modal container to:
<div class="modal fade" id="Pendingnote{{$newprovider->id}}" role="dialog">
All modal-triggering buttons have data-target="#Pendingnote", which means they all trigger the first modal with that id they can find.
You could try for instance removing the data-target attribute and trigger the modal with a bit of jQuery, like:
$('button[data-toggle]').on('click', function () {
$(this).next('form').find('.modal').modal('show');
});
I would suggest anyway to have the forminside the modal instead. So the jQuery would be:
$('button[data-toggle]').on('click', function () {
$(this).next('.modal').modal('show');
});
Lastly, take a look at this: http://getbootstrap.com/javascript/#modals-related-target
I'm having a register form and login form on my home page
the login form is activated when you press the login link (via bootstrap)
and the register form is visible all the time
now when one off those fails, I wanna send the error's to the page
but I can only access the var's of these:
return Redirect::to('/')->withErrors($validator)
->withInput(Input::except('password'));
but when I add ->with('method', 'login') I can't check in my home page the var $method
it's empty and never get's any values
can someone tell me what I might be doing wrong?
and also how I can activate the login form with variables that I send with the ->with()
the login form is made up like this:
<div class="modal fade bs-example-modal-sm pvvoModal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Login to your account</h4>
</div>
{{ Form::open(array('url' => 'admin/login', 'class'=>'form', 'role' => 'form')) }}
<div class="modal-body">
#if ( $errors->first('username') != null || $errors->first('password') != null)
<div class="alert alert-danger">
<p>Some errors occured</p>
{{ $errors->first('username') }}
{{ $errors->first('password') }}
</div>
#endif
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-user"></span></span>
{{ Form::text('username', $value = null, array('placeholder' => 'Username', 'class'=> 'form-control', 'required' => 'required', 'autofocus' => 'autofocus' )) }}
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-asterisk"></span></span>
{{ Form::password('password', array('placeholder' => 'Password', 'class' => 'form-control', 'id'=>'password', 'required' => 'required')) }}
</div>
</div>
<div class="text-center">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
{{ Form::submit('Login', array('class' => ' btn btn-primary')) }}
</div>
</div>
{{ Form::close() }}
</div><!-- /.modal-content -->
</div>
</div>
</div>
The with() method flashes data to the session, you have to retrieve your data using Session::get()method.
Source : Laravel doc
Fixed it by doing it via AJAX :)
was easier for keeping the login form open