ArgumentCountError when updating content in larvel

Viewed 25

In my laravel application, I'm trying to update some content using the following controller function. But every time when I try to run the following function am getting an error

public function update_tcp(Request $request, $id)
{
        
    try{

        Session::put('tcpSession', '1');
        $request->merge(['gender' => 'M']);
        $this->validate($request, [
            'first_name_tcp2' => 'required',
            'last_name_tcp2' => 'required',
            'image_id' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'user_id'=>'required',
            'gender'=>'required',
            'date_of_birth_tcp2'=>'required'
        ]);
    
        //$input = $request->except('_method', '_token');

        $input = $request->all();

        unset($input['_token']);
        unset($input['_method']);

        if ($image = $request->file('image_id')) {
            $destinationPath = 'propics/';
            $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
            $image->move($destinationPath, $profileImage);
            $input['image_id'] = "$profileImage";
            //dd($profileImage);

        }else{
            //unset($input['image']);
            $profileImage='default-avatar.png';
            $request->merge(['image_id' => 'default-avatar.png']);

        }
        $data = $request->input();
        $tcp = TakeCarePerson::WHERE('id','=',''.$id.'');

        $tcp->first_name = $data['first_name_tcp2'];
        $tcp->last_name = $data['last_name_tcp2'];
        $tcp->date_of_birth = $data['date_of_birth_tcp2'];
        $tcp->user_id = $data['user_id'];
        $tcp->image_id = $profileImage;
        $tcp->gender= $data['gender'];
    
        $tcp->update();
    
        return redirect()->route('participants.index')
    
                        ->with('success',__('texts.Take care person updated successfully.'));
    } catch(Exception $e){
        return back() ->with('failedTcp',__('texts.Le fichier sélectionné doit être une image.'));
    }
}

This is the error,

ArgumentCountError Too few arguments to function Illuminate\Database\Eloquent\Builder::update(), 0 passed

Where do I need to fix in order to function my update function properly?

1 Answers

You are not passing data to update .Instead where find by id and then call save()

$tcp = TakeCarePerson::find($id);

$tcp->first_name = $data['first_name_tcp2'];
$tcp->last_name = $data['last_name_tcp2'];
$tcp->date_of_birth = $data['date_of_birth_tcp2'];
$tcp->user_id = $data['user_id'];
$tcp->image_id = $profileImage;
$tcp->gender= $data['gender'];
$tcp->save();
    

or

$tcp = TakeCarePerson::where('id','=',$id)->update([
        'first_name'=> $data['first_name_tcp2'],
        'last_name'=>$data['last_name_tcp2'],
         'date_of_birth'=>$data['date_of_birth_tcp2'],
         'user_id'=>$data['user_id'],
         'image_id'=>$profileImage,
         'gender'=>$data['gender']
    ]);
Related