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
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 have succesfully created an application within Laravel which allows users to edit posts for a item. The current method is by redirecting user to different page where they are able to make changes. However to increase the user expirence I was planning to add 'modal' within the page. Meaning that editing posts will happen within the same page. This method for me partly works. In a aspect that the last or the latest post is only editable post. If I click on any of the posts it makes me only make changes to the latest post. Any help, suggestions or examples are trully appreciated.
Below I have attached my show.blade.php which shows the item infromation and posts.
<ul class="list-group">
#foreach ($car->reviews as $review)
<li class="list-group-item">
<strong>
Created by: {{ $review->user->name}} {{ $review->created_at->diffForHumans() }}:
<br>
Updated by: {{ $review->user->name}} {{ $review->updated_at->diffForHumans() }}:
</strong>
<a href="/reviews/{{ $review->id }}/edit" data-toggle="modal" data-target="#myModal1" >{{ $review->reviewbody }}</a>
<a style="float:right;" href="/reviews/{{$review->id}}/delete">Delete</a>
#endforeach
The Modal
<div class="container">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1">TEST</button>
<!-- Modal -->
<div class="modal fade" id="myModal1" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="review-edit">
<div class="review-edit-block">
{{ $review->reviewbody }}
<form action="/reviews/{{$review->id}}" method="POST">
{{ csrf_field() }}
{{ method_field('patch') }}
<div class="form-group">
<textarea style="position:relative;left:10%;width:375px;"name="reviewbody" class="form-control">{{ $review->reviewbody }}</textarea>
</div>
<div>
<button type="submit" class="btn btn-primary">Update Review</button>
</form>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
If the modal is included after the foreach loop, the variable $review will still be set to the value of the last item in the array.
An option is to use Javascript to handle the clicked link, set the specific data into the modal fields and then make the modal visible.
Example
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 have created a view that shows all images used in Carousel. This is my method in controller
My edit method:
public function edit($id)
{
$carousel = Carousel::find($id)->first();
return view('admin.partials._edit-modal.blade');
}
My route for edit:
Route::get('carousel.edit/{id}',['as' => 'carousel.edit', 'uses' => 'LinkController#edit']);
This is my code to display it in view:
#foreach($carousels as $carousel)
<div class="col-xs-6 col-md-3 grid">
<a href="{{ url($carousel->path) }}" class="thumbnail" data-lity>
<img class="img-responsive" src="{{ $carousel->path }}" alt="">
</a>
</div>
#endforeach
I've set up a button to open a model that shows the selected image and form to edit the image name [ Form not setup yet, shows only image].
My model code:
<div class="modal fade" id="myModal" 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">Modal Header</h4>
</div>
<div class="modal-body">
<img class="img-responsive" src="{{ $carousel->path }}" alt="">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
There is no error in code but when the edit button is clicked it is always showing the last image of the table. Whether I click edit button of first,second or any image. The model always shows the last image.
When showing the images with a foreach() loop you should include the button to edit that image including the id of the image and pass this id to the modal. Something like this:
<button type="button" class="edit-image" data-toggle="modal" data-target="#myEditImageModal" data_value="{{ $image->id }}"> Edit</button>
Then add a script to pass that specific id when the button is clicked to the modal. You can accomplish this with js:
<script>
// pass image id to modal
$('.edit-image').click(function() {
$("#image_id").val($(this).attr('data_value'));
});
</script>
Finally the modal will receive that id and you can store it in a hidden input field inside your form. Something like this:
<div id="myEditImageModal" tabindex="-1" role="dialog" class="modal fade">
...
<form>
...
<input type="hidden" name="image_id" id="image_id">
...
</form>
...
</div>
Place the modal outside the foreach loop.
In Laravel I have a table that contains an edit link on every row like the following;
<a data-toggle="modal" href="{{ route('myrow.edit', ['id' => $value['id']]) }}" data-target="#myModal">
<i class="fa fa-btn fa-pencil"></i>
</a>
The modal screen was defined as follows;
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content"></div>
</div>
</div>
When the previously mentioned button is clicked the edit.blade.php included looks like this;
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">My modal title</h4>
</div>
{!! Form::model($myrow, array('route' => array('mycontroller.update', $myrow->id), 'method' => 'PUT')) !!}
<div class="modal-body">
<!-- All kinds of fields -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button class="btn btn-success" type="submit">Save</button>
</div>
{!! Form::close() !!}
The mycontroller.update that was invoked looks like this;
public function update(Request $request, $id) {
$mymodel = MyModel::find($id);
$mymodel->user = $request->user()->id;
$mymodel->field = $request->field;
if (!$mymodel->save()) {
$errors = $mymodel->getErrors();
return redirect()
->action('MyController#index')
->with('errors', $errors)
->withInput();
}
return redirect()
->action('MyController#index')
->with('message', $mymodel->field . ' was added');
}
When I fill in data in my modal screen and submit it all works very well. The problem starts when I either press Cancel, press the "x" that closes the modal or simply click outside of the modal. In those situations the modal will obviously close but when I click an edit link in whatever other row it will always open the modal screen for the previously unsaved row until I refresh the screen.
Apparently I need some processing after the modal window was closed in any way but how should I do that. Does someone have a good strategy for this?
Update: it also doesn't perform a second GET request. The first time I click the edit link it retrieves the correct data with
GET https://example.com/mylaravelapp/mymodel/1/edit
The second (and any subsequent) time no request is made at all.
You'll need to use JavaScript to clear the form fields and repopulate them depending on the row you clicked. The modal still exists in the Dom after being closed that's why you're seeing the previously entered values
The problem there is with Bootstrap Modal's referencing. You are creating one unique Modal. The button that toggles a Modal uses the data-target="#myModal" attribute to reference the html that will popup when you click the button. That part of html is identified by id="myModal".
So, if you want to create multiple Modals you will need something like this:
Change data-target attribute's value
<a data-toggle="modal" href="{{ route('myrow.edit', ['id' => $value['id']]) }}" data-target="#myModal_{{$value['id']}}">
<i class="fa fa-btn fa-pencil"></i>
</a>
Then edit the Modal's html definition
<div class="modal fade" id="myModal_{{$value['id']}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content"></div>
</div>
</div>
However, you should not create multiples Modals; you must just create only one Modal, and use Javascript to update the Modals content and form's action attribute.