skip to Main Content

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.

2

Answers


  1. 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');
    }
    
    Login or Signup to reply.
  2. 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.');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search