Home > Blockchain >  laravel error "ArgumentCountError Too few arguments to function App\Http\Controllers\UserCon
laravel error "ArgumentCountError Too few arguments to function App\Http\Controllers\UserCon

Time:09-21

I'm trying to send a message to users through their user_id this is my controller

  public function messagesend(Request $request, $user_id){
        $data = array('details'=>$request->details,
        'email' =>$user_id->email);
        Mail::send('transactionmessage', $data, function($message) use ($data){
                $message->from ('[email protected]');
                $message->to($request->email);
                $message->subject($data['details']);
                
        });
          // echo $$request;
        return redirect('ongoingstatus')->with('success','Message Sent!');
    }

and my route is

Route::post('/messagesend', [UserController::class, 'messagesend'])->middleware(['auth'])->name('messagesend');

don't know why i'm getting this error

Too few arguments to function App\Http\Controllers\UserController::messagesend(), 1 passed in /home/swit/public_html/manager/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 and exactly 2 expected

this is my blade.php

<form method="POST" action = "{{route('messagesend')}}">
                 @csrf
                 <input type = "hidden" value="{{$email}}" name ="email" />
                <div class="row">
                    <div class="col-md-8">
                        <div class="tab-content profile-tab" id="myTabContent">
                            <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
                                  
                        <textarea placeholder="Enter Message" name="details" rows="5" cols="60"  id = "details"></textarea><br>
 
                                  <button type = "submit" class = 'btn btn-outline-primary mr-2'>Send Message</button>
                                        
                            </div>
                            
                        </div>
                    </div>
                </div>
            </form>  

please help me out if you know where i'm wrong thanks

CodePudding user response:

Your route doesn't take any parameter:

Route::post('/messagesend', [UserController::class, 'messagesend'])->middleware(['auth'])->name('messagesend');

However, the controller method needs two:

public function messagesend(Request $request, $user_id){
        $data = array('details'=>$request->details,
        'email' =>$user_id->email);
        Mail::send('transactionmessage', $data, function($message) use ($data){
                $message->from ('[email protected]');
                $message->to($request->email);
                $message->subject($data['details']);
                
        });
          // echo $$request;
        return redirect('ongoingstatus')->with('success','Message Sent!');
    }

The first argument, Request $request, is injected by Laravel using dependency injection.

The second needs to be retrieved from the route, but... their is no parameters.

You have two options here, depending of what you are trying to achieve:

  • Adding a parameter to your route:
Route::post('/messagesend/{user_id}', [UserController::class, 'messagesend'])->middleware(['auth'])->name('messagesend');
  • Remove the argument $user_id from your controller method.

CodePudding user response:

All the form does is POST a Request $request to your Route. You aren't passing the messagesend() function a $user_id.

  • Related