Sumeet
Sumeet

Reputation: 121

Error while updating data in Laravel

Unable to update the data,

Controller Code

public function update(Request $request, $id)
    {
        //

         echo $request->all();
       /* $user= User::find($id);
        $user->update($request->all());
         $user->save();
         return redirect('user');*/
    }

data not getting updated that's why used echo.

When used

echo $request->all();     (line 86)

got the error

 (1/1) ErrorException
    Array to string conversion
    in userController.php (line 86)

used

 dd($request->all());

got result

array:14 [▼
  "_method" => "PUT"
  "_token" => "zDkzEpJCKscUgAGvgFQwu6gKbtRwm8N8MdBHC9em"
  "userType" => "Admin"
  "firstName" => "Sda"
  "lastName" => "ad"
  "gender" => "M"
  "personalContact" => "123"
  "officeContact" => "132"
  "email" => "ads"
  "country" => "ds"
  "state" => "ds"
  "city" => "ddsf"
  "address" => "dsf"
  "zipCode" => "1234"
]

my model

class User extends Model
{
    //
    protected $fillable = [
        'userType', 'firstName', 'lastName', 'gender','personalContact','officeContact','email','country','state','city','address','zipCode'
    ];
/*  public $incrementing = false;*/
}

User table Migration

 Schema::create('users', function (Blueprint $table) {
            $table->increments('Id');
            $table->uuid('userType'); 
            $table->string('firstName');
            $table->string('lastName');
            $table->char('gender');
            $table->string('personalContact');
            $table->string('officeContact');
            $table->string('email');
            $table->uuid('country');
            $table->uuid('state');
            $table->uuid('city');
            $table->string('address');
            $table->integer('zipCode');
            $table->rememberToken();
            $table->timestamps();
        });

can anyone please let me know, What I have done wrong

Upvotes: 0

Views: 155

Answers (1)

user320487
user320487

Reputation:

You need to use $request->only(['userType, 'firstNAme', 'lastName']) etc etc etc when filling your model. You cannot pass in the indexes like _method and _token, otherwise it will attempt to save those to the database and not know what to do with them.

Upvotes: 1

Related Questions