1

I'm using Laravel 4.2.* I have a User Model where insert() method is working normally but when I'm using create() method it is throwing an error:

Array to string conversion

My model:

class User extends Eloquent{

    public $table = 'users';
    protected $primaryKey = 'user_id';
    protected $fillable = ['first_name', 'last_name', 'created_by'];

    public function insertUserInfo($data)
    {
        //return self::insert($data); //working with no error but not adding `created_at`
        return self::create($data); //Array to string conversion
    }
}

Sample data:

$data = [
    'first_name' => 'Jhon',
    'last_name' => 'Doe',
    'created_by' => Auth::id() // var_dump(Auth::id()) => int(11)
];

It 'll be helpful if anyone can help me as why create() is throwing an error.

PHP v5.6.3

EDITED
I can assure that there is no array value inside $data. and the same $data is working with insert() but throwing error when using create() or save() method.

16
  • var_dump($data) maybe something isn't what you think it should be. Commented Feb 15, 2017 at 15:06
  • @aynber: Ya I have checked that but but all the fields are as expected, moreover same dataset is working with insert method. Commented Feb 15, 2017 at 15:11
  • Maybe Auth::id() is returning an array ... Commented Feb 15, 2017 at 16:48
  • @NetGuy: var_dump(Auth::id()) => int(11). Commented Feb 15, 2017 at 16:56
  • Why don't you use $user = new User($data); then $user->save(); Commented Feb 15, 2017 at 17:02

1 Answer 1

1

Add array type hinting:

 public function insertUserInfo(array $data)
 {
    return self::create($data);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

tried this, but with success, still getting an error.

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.