php - 数组到字符串的转换(使用 json_encode 方法时出现 SQL 错误)

标签 php mysql laravel laravel-5.5

我收到数组到字符串转换错误。 即使我尝试使用 serialize 和 json_encode 方法将数组转换为 string 。 但它没有用。我不知道这个问题是针对那个字符串还是其他变量,。 这是我的问题:

数组到字符串的转换

SQL:

insert into vehicledetails (vehicle_id, shortName, registration, vehicleType, vehiclemake, modelyear, documentType, dri_id, expDate, notiInterval, `user_id`, document, updated_at, created_a`)

values (w879847, 9e87sdf98, 9s8f7sd9f8, Car, sdjkfhskfh, NULL, Insurance, , , , 9266, ["dfusf9sf89.jpg","9sd8fsudfsd9.png"], 2019-08-07 13:17:41, 2019-08-07 13:17:41)

Controller :

        $request['user_id'] = Auth::user()->id;
                 $vehiDetails=$request->all();
                 $file = Input::file('file');

                if($file){
                    foreach($request->file('file') as $file) {
                        $exists = Storage::disk('spaces')->exists('FMSuploads/VehicleDoc/'.$file->getClientOriginalName());
                        if($exists){
                            \Session::flash('error', 'File name already exist');
                            return redirect()->back()->withInput();
                        }
                       else{
                        Storage::disk('spaces')->putFileAs('FMSuploads/VehicleDoc', request()->file, $file->getClientOriginalName(), 'public');
                         log::info('document uploaded successfullly');
                       }
                        $array[] = $file->getClientOriginalName();
                    }
                }else{
                    $vehiDetails['document'] = "";
                }
            }
                $array = json_encode($array);
                $vehiDetails['document'] = $array;
            VehicleDetails::create($vehiDetails);

模型页面。

class VehicleDetails extends Model
{
    protected $connection = 'mysql2';
        protected $table = 'vehicledetails';

     public $primaryKey = 'id';


    //public $timestamps = false;

    protected $fillable = array('user_id','vehicle_id','shortName','registration','morningTripStartTime','eveningTripStartTime','dri_id','vehicleType','vehiclemake','modelyear','documentType','document','expDate','notiInterval');


    protected $guarded = array();

}

最佳答案

在 for 循环外声明 $docLink = ''; 在 for 循环内 $doclink .= $file->getClientOriginalName().','; 下面的 for 循环 $vehiDetails['document'] = $doclink; 希望这有助于解决问题

关于php - 数组到字符串的转换(使用 json_encode 方法时出现 SQL 错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57395527/

相关文章:

PHP 不支持 SQL;空结果

php - 使用keyby集合方法检索eloquent api资源

php - 如何将此 SQL 语句转换为 FluentPDO 代码

php - 将平面数组转换为分层的多维数组

php - 提交表单时无法更新sql表

php - Laravel:加载存储在 'storage' 文件夹中的图像

laravel - PhpWord TemplateProcessor 克隆表行并在其中插入信息

php - 如何存储由用户服务器端(mySQL)创建的 <li>-item

php - 如何使用多个实体绑定(bind)设置 ElasticSearch 索引结构

mysql - 在一个脚本中执行所有存储过程