如何在Laravel中使用数据库中的动态内容显示模态?
我的观点:
<li><a href="{!! action('TestsController@show', $test->slug) !!}">{!! $test->test_name !!} </a></li>
我的模特:
public function show($slug)
{
$test = Test::whereSlug($slug)->firstOrFail();
return view('tests.show', compact('test'));
}
我希望在活动页面上显示此模式,而不是创建新视图。我想可以使用return view()->with
完成但无法实现它。
答案 0 :(得分:10)
如果你愿意,你可以做到这一点。
控制器中的:
public function show($slug)
{
$test = Test::whereSlug($slug)->firstOrFail();
return view('tests.show', compact('test'));
}
并在您看来:
<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>
然后,如果您要获取多个数据,则只需使用foreach
。例如:
<强>控制器强>
public function show()
{
$test = Test::all();
return view('tests.show', compact('test'));
}
查看:强>
@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