php 单击 href Laravel 时显示引导模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32469873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show bootstrap modal when click on href Laravel
提问by Roman
How can I show modal with dynamic content from database in Laravel?
如何在 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.
我想在活动页面上显示此模式,而不是创建新视图。我想它可以完成return view()->with
但无法实现它。
回答by wobsoriano
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">×</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:
然后,如果您要获取多个数据,则只需使用foreach
. 例如:
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">×</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