Reputation: 31
The URL that sends the request is:
http://localhost:8000/my_details?my_id='.$id
My Controller looks like this:
public function my_action(Request $request){
$myauth=$request->validate([
'remarks' => 'required'
]);
$full_name = Auth::user()->full_name;
$id=$request->input('id');
$user_remarks=$request->input('user_remarks');
$remarks=$myauth['remarks'];
$status=$request->input('status');
$newremarks=$user_remarks . ' ' . $full_name . ': ' . $remarks .';';
$result=DB::table('my_record')->where('id',$id)->update([
'remarks'=>$newremarks,
'status'=>$status,
]);
if($result){
return redirect(route('my_details?my_id='.$id))->with('success', 'Action taken successfully.');
}
return redirect(route('my_details?my_id='.$id))->with('error','Remarks cannot be empty');
}
My Blade looks like:
<div>
@if(Session::has('error'))
<div class="w3-panel w3-red" role="alert">
{{ Session::get('error') }}
</div>
@endif
@if(Session::has('success'))
<div class="w3-panel w3-green" role="alert">
{{ Session::get('success') }}
</div>
@endif
</div>
I want the controller to send the success message or error message on the same blade view that sends the request.
Upvotes: 1
Views: 1326
Reputation: 17
public function my_action(Request $request){
$this->validate($request, [
'remarks' => 'required'
]);
$full_name = Auth::user()->full_name;
$id=$request->input('id');
$user_remarks=$request->input('user_remarks');
$remarks=$myauth['remarks'];
$status=$request->input('status');
$newremarks=$user_remarks . ' ' . $full_name . ': ' . $remarks .';';
$result=DB::table('my_record')->where('id',$id)->update([
'remarks'=>$newremarks,
'status'=>$status,
]);
if($result){
return redirect(route('my_details?my_id='.$id))->with('success', 'Action taken successfully.');
}
return redirect(route('my_details?my_id='.$id))->with('error','Remarks cannot be empty');
}
<input type="text" name="remarks">
@if($errors->has('remarks'))
<div class="error">{{ $errors->first('remarks') }}</div>
@endif
@if($errors->any())
{{ implode('', $errors->all('<div>:message</div>')) }}
@endif
this way you can handle ajax response too
$validator = Validator::make($requst->all(),
'remarks' => 'required'
);
if ($validator->fails())
{
return redirect(route('my_details?my_id='.$id))->withErrors($validator);
}
Upvotes: 0
Reputation: 66
So if you want to a to return back to a previous route with a fail/success message, you need to use redirect with payload.
Route examples:
For redirection:
return redirect()->route('Communities.Index');
For redirection to previous route: return redirect()->back();
For redirection with payload:
return redirect()->route('Communities.Index')->with('success', 'Item added to cart successfully!');
return redirect()->back()->with('error', 'This is an error message.');
Upvotes: 2
Reputation: 51
public function my_action(Request $request){
$myauth=$request->validate([
'remarks' => 'required'
]);
$full_name = Auth::user()->full_name;
$id=$request->input('id');
$user_remarks=$request->input('user_remarks');
$remarks=$myauth['remarks'];
$status=$request->input('status');
$newremarks=$user_remarks . ' ' . $full_name . ': ' . $remarks .';';
$result=DB::table('my_record')->where('id',$id)->update([
'remarks'=>$newremarks,
'status'=>$status,
]);
if($result){
return redirect()->route('my_details',$id)->with('success', 'Action taken successfully.');
}
return redirect()->route('my_details',$id)->with('error','Remarks cannot be empty');
}
Upvotes: 1