Array to string conversion (SQL Error while using json_encode method)

252 Views Asked by At

I'm getting array to string conversion error . Even I tried with serialize and json_encode method to convert an array to string . But it didn't work . I don't know whether this issue is for that string or other variables, . Here is my issue :

Array to string conversion

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);

Model page .

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();

}
1

There are 1 best solutions below

0
Nilkamal Gotarne On BEST ANSWER

Outside for loop declare $docLink = ''; inside for loop $doclink .= $file->getClientOriginalName().','; below for loop $vehiDetails['document'] = $doclink; Hope this helps as a work around