4

How can I show modal with dynamic content from database in Laravel?

my view:

<li><a href="{!! action('TestsController@show', $test->slug) !!}">{!! $test->test_name !!} </a></li> 

my model:

public function show($slug) { $test = Test::whereSlug($slug)->firstOrFail(); return view('tests.show', compact('test')); } 

This modal I want to show on active page instead of creating new view. I guess it could be done with return view()->with but can not implement it.

2
  • You can do that with AJAX. To load your data but not refreshing the page. Commented Sep 9, 2015 at 2:11
  • Posible duplicate, please check this stackoverflow.com/questions/18378720/… Commented Sep 9, 2015 at 3:00

1 Answer 1

12

You can do this trick if you want.

in your controller:

public function show($slug) { $test = Test::whereSlug($slug)->firstOrFail(); return view('tests.show', compact('test')); } 

and in your view:

<li><button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#yourModal"></li> <div class="modal fade" id="yourModal" 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">&times;</span></button> <h4 class="modal-title" id="myModalLabel">{{$test->someTitle}}</h4> </div> <div class="modal-body"> {{$test->someField}} </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> 

And then if you have multiple data to get, you just have to use a foreach. For example:

controller

public function show() { $test = Test::all(); return view('tests.show', compact('test')); } 

view:

 @foreach ($test as $t) <li><button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#yourModal{{$t->id}}"></li> @endforeach @foreach ($test as $t) <div class="modal fade" id="yourModal{{$t->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">&times;</span></button> <h4 class="modal-title" id="myModalLabel">{{$t->someTitle}}</h4> </div> <div class="modal-body"> {{$t->someField}} </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> @endforeach 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks mate! Nice and clean solution, works perfectly.
Glad I could be of help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.