0

I use Laravel version 5.1 and I need ideas how to output a specific dynamic variable(data-target) outside of foreach.

The modal div(bootstrap modal) it must be outside of foreach!

What is the best to use php or jquery and how to solve this problem?

Html and Laravel(php):

@foreach ($category as $name => $value)
<li>
<a href="">{{$value->category_name}}</a>
<button type="button" class="btn btn-warning fa fa-pencil-square-o pull-right easy" data-toggle="modal" data-target="#{{$value->id}}"></button>
...
</li>
@endforeach

<div class="modal fade" id="{{$value->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
...
</div>
4
  • Why it should be outside of foreach? Commented Nov 6, 2016 at 10:07
  • You can use another foreach for that specific element. Commented Nov 6, 2016 at 10:09
  • Because I need the specific and same id getting when I click the button. My button and the modal div must have the same id somehow. Commented Nov 6, 2016 at 10:24
  • @VasilisGreece, so you can create modals with the same or additional foreach. If there are just few modals and no heavy data loaded (like graphics or big texts), it's easier to do this with foreach rather than with AJAX. Commented Nov 6, 2016 at 11:27

2 Answers 2

1

The best and simpliest way to achieve this is to create a new Route, depending on the ID of the category.

Then, create a view containing the modal content called from the controller of this Route.

After this, call a jQuery script when clicking on this edit button, which loads the modal view via an AJAX request (depending on the ID). When this request is done, insert the content into the modal and open the modal.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes you are right I think that is the only solution. Now I need to learn how to use AJAX requests.
0

I found the solution. No AJAX required or additional foreach(to prevent heavy loaded html data). It works perfectly!

jquery:

$(document).ready(function(){
    $("#categoryview .fa-pencil-square-o").click(function(){
        $(this).removeAttr("data-target").attr("data-target", "#edit");
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.