简体   繁体   中英

Laravel 5.4 Query Builder Array to string conversion

I'm trying to make a laravel 5.4 command to import data from Excel. I could gather all the data and now I'm trying to insert in the database but I'm getting a weird error.

This is the piece of code weher the error happens:

$data = [
            'name' => $row['name'],
            'address' => $row['address'],
            'suite' => $row['suite'],
            'city' => $row['city'],
            'zipcode' => $row['zipcode'],
            'country_code' => $row['country_code'],
            'province_code' => $row['province_code'],
            'region_id' => $region_id,
            'phone' => $row['phone'],
            'fax' => $row['fax'],
            'website' => $row['website'],
            'linkedin' => $row['linkedin'],
            'facebook' => $row['facebook'],
            'twitter' => $row['twitter'],
            'number_employees' => $row['number_employees'],
            'year_creation' => $row['year_creation'],
            'turnover_id' => $turnover_id,
            'status' => 1,
            'hidden_directory' => 0,
            'hidden_directory_byadmin' => 0,
            'is_member' => $row['is_member'],
            'created_by' => 1,
        ];

        $company = Company::create($data);

And this is the error:

   Array to string conversion (SQL: insert into `company` (`name`, `address`, `suite`, `city`, `zipcode`, `country_code`, `province_code`, `region_id`, `phone`, `fax`, `website`, `lin  
  kedin`, `facebook`, `twitter`, `number_employees`, `year_creation`, `turnover_id`, `status`, `hidden_directory`, `created_by`, `updated_at`, `created_at`) values (Test Company 3, 1  
  315 Rue St-Louis, , Montreal, H4L 2P4, CA, QC, , +1 (438) 999-555, +1 (438) 999-555, http://www.google.com, https://www.linkedin.com/feed/, https://www.facebook.com/, https://twitt  
  er.com/, 200, 2010, , 1, 0, 1, 2019-03-27 09:48:29, 2019-03-27 09:48:29)) 

It's like it's missing the ticks in the SQL insert query. Weird is that I do the same insert in other parts of the system where this doesn't happen.

I'm not trying to insert JSON here. Yes, I've seen all similar questions here in StackOverflow and it's common to happen when you are trying to insert json, but not regular strings and ints.

What I may be doing wrong here?

Thanks for any help.

Essentially, an index in $row is an array , and not a string or an int , as is expected for insert. Since this could be any index, it's best to try to avoid this error using a try/catch block with DB::transaction() logic:

\DB::beingTransaction();

try {
    $data = [
        'name' => $row['name'],
        'address' => $row['address'],
        'suite' => $row['suite'],
        'city' => $row['city'],
        'zipcode' => $row['zipcode'],
        'country_code' => $row['country_code'],
        'province_code' => $row['province_code'],
        'region_id' => $region_id,
        'phone' => $row['phone'],
        'fax' => $row['fax'],
        'website' => $row['website'],
        'linkedin' => $row['linkedin'],
        'facebook' => $row['facebook'],
        'twitter' => $row['twitter'],
        'number_employees' => $row['number_employees'],
        'year_creation' => $row['year_creation'],
        'turnover_id' => $turnover_id,
        'status' => 1,
        'hidden_directory' => 0,
        'hidden_directory_byadmin' => 0,
        'is_member' => $row['is_member'],
        'created_by' => 1,
    ];

    $company = Company::create($data);

    \DB::commit();
} catch(\Exception $ex){
   \Log::error("Error in Company::create(): ".$ex->getMessage());
   \DB::rollBack();
}

If you're unsure of what $row[...] can contain, and there's the possibility of an array /otherwise invalid value, using a try/catch like that will prevent insert issues, and only finalize the insert when no errors are encountered. Beyond that, it is up to you to ensure the data being inserted is what you expect.

you can do style conversion of string like this

$data = [
    'name' => (string)$row['name'],
    'address' => (string)$row['address'],
    'suite' => (string)$row['suite'],
    'city' => (string)$row['city'],
    'zipcode' => (string)$row['zipcode'],
    'country_code' => (string)$row['country_code'],
    'province_code' => (string)$row['province_code'],
    'region_id' => $region_id,
    'phone' => (string)$row['phone'],
    'fax' => (string)$row['fax'],
    'website' => (string)$row['website'],
    'linkedin' => (string)$row['linkedin'],
    'facebook' => (string)$row['facebook'],
    'twitter' => (string)$row['twitter'],
    'number_employees' => (string)$row['number_employees'],
    'year_creation' => (string)$row['year_creation'],
    'turnover_id' => $turnover_id,
    'status' => 1,
    'hidden_directory' => 0,
    'hidden_directory_byadmin' => 0,
    'is_member' => (bool)$row['is_member'],
    'created_by' => 1,
];

$company = Company::create($data);

OR instead of string you can use int/float. That will save your data in string format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM