如何在 Laravel 中使用 SweetAlert 发出警报
How to make alert with SweetAlert in Laravel
我想使用 SweetAlert 来显示我的数据。
我完成了我的任务
public function registration()
{
Gate::authorize('admin-level');
$url = URL::signedRoute(
'register',
now()->addMinutes(20));
return redirect('users')->with('success','$url');
}
以及与之相伴的路线
Route::get('registration', [App\Http\Controllers\UserController::class, 'registration'])->name('registration');
问题 与消息有关,因为我使用 composer 下载了 SweetAlert,我应该可以让一切正常,但是当我尝试执行我的 class 按钮时路线:
<a href="{{route('registration')}}"><button type="button" class="btn btn-outline-primary">{{ __('Registration link') }}</button></a>
@if(session('succes_message'))
<div class= "alert alert-succes">
{{session('succes_message')}}
</div>
@endif
什么都不弹出(应该的时候)
它可能有什么问题?
当您使用 ->with()
时,这意味着将项目存储在 session 中以供下一个请求使用。
return redirect('users')->with('success', '$url');
Here comes the question. What do you do after this?
Create a notification information or an alert (popup with SweetAlert)?
如果将其用作通知,您的代码没有问题。如果你想做alert(popup with SweetAlert),你的理解是错误的。
仅仅因为您使用的 class 使用名称 alert
,并不意味着它会使用 SweetAlert 发出警报。
要使用 SweetAlert,您可以在 header 或 </body>
标签之前添加 JavaScript:
<script>
@if($message = session('succes_message'))
swal("{{ $message }}");
@endif
</script>
或使用SweetAlert2:
<script>
@if($message = session('succes_message'))
Swal.fire(
'Good job!',
'{{ $message }}',
'success'
)
@endif
</script>
如果您对将脚本放在特定 blade 视图中感到困惑,请 read my answer here。
我想使用 SweetAlert 来显示我的数据。
我完成了我的任务
public function registration()
{
Gate::authorize('admin-level');
$url = URL::signedRoute(
'register',
now()->addMinutes(20));
return redirect('users')->with('success','$url');
}
以及与之相伴的路线
Route::get('registration', [App\Http\Controllers\UserController::class, 'registration'])->name('registration');
问题 与消息有关,因为我使用 composer 下载了 SweetAlert,我应该可以让一切正常,但是当我尝试执行我的 class 按钮时路线:
<a href="{{route('registration')}}"><button type="button" class="btn btn-outline-primary">{{ __('Registration link') }}</button></a>
@if(session('succes_message'))
<div class= "alert alert-succes">
{{session('succes_message')}}
</div>
@endif
什么都不弹出(应该的时候)
它可能有什么问题?
当您使用 ->with()
时,这意味着将项目存储在 session 中以供下一个请求使用。
return redirect('users')->with('success', '$url');
Here comes the question. What do you do after this?
Create a notification information or an alert (popup with SweetAlert)?
如果将其用作通知,您的代码没有问题。如果你想做alert(popup with SweetAlert),你的理解是错误的。
仅仅因为您使用的 class 使用名称 alert
,并不意味着它会使用 SweetAlert 发出警报。
要使用 SweetAlert,您可以在 header 或 </body>
标签之前添加 JavaScript:
<script>
@if($message = session('succes_message'))
swal("{{ $message }}");
@endif
</script>
或使用SweetAlert2:
<script>
@if($message = session('succes_message'))
Swal.fire(
'Good job!',
'{{ $message }}',
'success'
)
@endif
</script>
如果您对将脚本放在特定 blade 视图中感到困惑,请 read my answer here。