How to send custom Error and Success message from Controller to View in Laravel 7
Laravel 7 makes it easier when we need to send any kind of success or error message or any kind of custom message from our controller to blade ( view ) file . So in this article we will learn how to send custom message from controller to view file in Laravel .
To understand this we will take an example to understand and we will use " ->with() " function provided Laravel to send message from controller to view file . We will use bootstrap dismissable alert box for showing our custom message .
Basic Usage :
controller :
return redirect(url('/form'))->with('successmessage','data saved successfully');
view file :
@if(session('successmessage'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{session('successmessage')}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endif
Send Form validation Error :
We can also send validation error from controller to the blade file . If your form input does not matches with your validation code then it will automatically redirect to your blade file and execute your code for showing validation error .
checkController.php :
$this->validate($req,[
'name' => 'required|min:5|max:35',
'email' => 'required|email|unique:users',
'phone' => 'required|numeric',
'password' => 'required|min:2|max:20',
'address' => 'required|min:5|max:500',
'detail' => 'required|min:5|max:1000',
],[
'name.required' => ' The Name field is required.',
'email.min' => ' Enter a proper email address',
'phone.min' => ' Phone should be atleast 10 digits',
'phone.max' => ' Phone should maximum 10 digits',
'password.required' => 'password field is required',
'address.required' => ' Address field is required',
'detail.required' => ' Detail field is required',
'detail.min' => ' Enter atleast 5 characters',
]);
form.blade.php :
@if(count($errors))
@foreach($errors->all() as $error)
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{$error}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endforeach
@endif
Example :
This example will make you understand that how can you send both success and error and custom message using " ->with() " function in Laravel as well as you will get to know to know how to use form validation and how to show form validation errors with dismissable alerts .form.blade.php :
@extends('layouts.app')
@section('content')
<form class="container" action="{{url('/store')}}" method="post">
{{csrf_field()}}
@if(count($errors))
@foreach($errors->all() as $error)
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{$error}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endforeach
@endif
@if(session('successmessage'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{session('successmessage')}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endif
<label>Name</label>
<input type="text" name="name" class="form-control">
<label>E-Mail</label>
<input type="email" name="email" class="form-control">
<label>Password</label>
<input type="password" name="password" class="form-control">
<label>Address</label>
<input type="address" name="address" class="form-control">
<label>Phone No.</label>
<input type="number" name="phone" class="form-control">
<label>About</label>
<textarea class="form-control" rows="3" name="detail"></textarea>
<button type="submit" class="btn btn-primary">submit</button>
</form>
@endsection
checkController.php :
public function store(Request $req)
{
$this->validate($req,[
'name' => 'required|min:5|max:35',
'email' => 'required|email|unique:users',
'phone' => 'required|numeric',
'password' => 'required|min:2|max:20',
'address' => 'required|min:5|max:500',
'detail' => 'required|min:5|max:1000',
],[
'name.required' => ' The Name field is required.',
'email.min' => ' Enter a proper email address',
'phone.min' => ' Phone should be atleast 10 digits',
'phone.max' => ' Phone should maximum 10 digits',
'password.required' => 'password field is required',
'address.required' => ' Address field is required',
'detail.required' => ' Detail field is required',
'detail.min' => ' Enter atleast 5 characters',
]);
$data=new Detail;
$data->name=$req->name;
$data->email=$req->email;
$data->phone=$req->phone;
$data->password=Hash::make($req->address);
$data->address=$req->address;
$data->detail=$req->detail;
$data->save();
if($data->save())
{
return redirect(url('/form'))->with('successmessage','data saved successfully');
}
}
Output :
On success :
On Validation Error :
Thank you for reading this article 😊
For any query do not hesitate to comment 💬
Also Read :
Types of Migration in Laravel
How to use multiple where condition in Laravel
how to Use Laravel 7 Auth
How to make Covid-19 Tracker
