简体   繁体   中英

Laravel SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into `add_file` (`0`)

I need to submit a form with multiple files to the database in Laravel,each time i fill the form i got this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into add_file ( 0 ) values ({"name":"Opeyemi Adam","description":"Thanks bro","attach":[["CRITICAL TECHNICAL OBSERVATIONS.doc"]]}))

Below is the Model

class AddFile extends Model{
   protected $table = 'add_file';
   protected $fillable = ['name', 'description', 'attach'];
}

Controller

public function submitform(AddFileFormRequest $request){
    $destinationPath = storage_path('app/attachments/');
    $attach_file =  array();


    $file = $request->file('attach');
    if (!empty($file[0])){
        foreach($file as $key){
            $filename = $key->getClientOriginalName();

            $key->move($destinationPath, $filename);

            $attach_file[] =  array($filename);
        }

    }


    $form_content = new AddFile(array(
        'name'          => $request->get('name'),
        'description'   => $request->get('description'),
        'attach'        => $attach_file
    ));


    var_dump($form_content);
    DB::table('add_file')->insert(array($form_content));


}

Don't know where the field list is coming from

Looks like you're trying to save an array ( $attach_file ) straight into a text column of some sort. You'll need to change it into a string first in order to do that - eg through serialize() or json_encode() .

Try changing you code just a little bit,

$form_content = array(
        'name'          => $request->get('name'),
        'description'   => $request->get('description'),
        'attach'        => $attach_file
    );


DB::table('add_file')->insert($form_content);

Or you can do,

$form_content = array(
        'name'          => $request->get('name'),
        'description'   => $request->get('description'),
        'attach'        => $attach_file
    );


AddFile::create($form_content);

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