I was to trying to show flash message with following controller. But it’s not displaying any message on view page.
public function deleteTag(Request $request)
{
DB::table('tags')
->where('id', $request->id)
->where('user_id', Auth::user()->id)
->delete();
Session::flash('error', 'Tag deleted successfully' );
return redirect('tags');
}
In view part:
@if (Session::has('error'))
<div class="alert alert-primary inline-form-alert d-flex align-items-center justify-content-between table-alert">
<ul>
<li class="alert-icon-content"><i class="bi bi-exclamation-circle-fill me-2"></i>{{ Session('error') }}</li>
</ul>
<button class="btn-close ms-3" type="button" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
In same page following controller flash message is showing. Why flash message not displaying on delete controller?
public function storeTag(Request $request)
{
$this->validate($request, [
'tag_name' => [
Rule::unique('tags')->where(function ($query) {
$query->where('user_id', '=', Auth::user()->id);
})
],
]);
$tag = new Tag();
$tag->tag_name = $request->Input(['tag_name']);
$tag->user_id = Auth::user()->id;
$tag->save();
Session::flash('error', 'Tag created successfully' );
return redirect('tags');
}
2
Answers
Here is the Docs Link
You can redirect to route with the flash message.
You need to check first that if data is delete successful if yes then send success otherwise send error
In the
view section
you can add this code and one thing you must be check that this code is need to write where theredirect('tags')
route load the view.Make sure that this line LaravelSanctumHttpMiddlewareEnsureFrontendRequestsAreStateful::class, stays commented or removed in the Kernel.php