0

I have this following problem while trying to Edit data already existing in my database. my controller name is UserController.php

Error: Creating default object from empty value.

I can Pull data from my database and post it in my form. when trying editing that error occurs.
Here is the block of code that causes the error.

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

    $this->validate($request, [
        'fname' => 'required',
        'lname' => 'required',
        'email' => 'required',
        'phone' => 'required',
        'address' => 'required',
        'country' => 'required',
        'city' => 'required',
        'bday' => 'required',
        'username' => 'required',
        'password' => 'required',
        'access' => 'required'
    ]);
    $userList = users::find($id);
    $userList->fname = $request->get('fname');
    $userList->lname = $request->get('lname');
    $userList->email = $request->get('email');
    $userList->phone = $request->get('phone');
    $userList->address = $request->get('address');
    $userList->country = $request->get('country');
    $userList->city = $request->get('city');
    $userList->bday = $request->get('bday');
    $userList->username = $request->get('username');
    $userList->password = $request->get('password');
    $userList->access = $request->get('access');
    $userList->save();
    return redirect()->route('users.index')->with('success', 'Data Updated');


}

I can see from the debugger that I send the new data

GET Data empty
POST Data
_token   "mnC6GliLHdSazZkEpaxZQ97aAChr2LObcc9clMlk"
_method  "PATCH"
fname    "test"
lname    "user"
email    "[email protected]"
phone    "12345678990"
address  "Streat"
country  "countryplace"
city     "somecity"
bday     "2018-01-01"
username     "tester"
password     "test"
access   "Client"

But It highlights $userList->fname = $request->get('fname'); and says : "Creating default object from empty value"

I am new to laravel and cant understand why this is happening. is it because of my form?

  <form method="post" action="{{action('UserController@update','$id')}}">
                    {{csrf_field()}}
              <input type="hidden" name="_method" value="PATCH" />

1 Answer 1

2

I think the problem is here:

<form method="post" action="{{action('UserController@update','$id')}}">

You should not use quotes for $id, it should be:

<form method="post" action="{{action('UserController@update',$id)}}">

Now because you have quotes, in line:

$userList = users::find($id);

no user is found, because in fact it's doing:

$userList = users::find('$id');
Sign up to request clarification or add additional context in comments.

1 Comment

Yes that was the case, I feel kinda ashamed to miss something like that. Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.