关于php:Laravel SQLSTATE [42S22]:找不到列:1054″字段列表”中的未知列” 0″(SQL:插入到” add_file”(” 0″)中)

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

我需要向Laravel中的数据库提交一个包含多个文件的表单,每次我填写表单时都会出现此错误

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"]]}))

下面是模型

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

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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));


}

不知道字段列表来自何处


好像您正在尝试将数组($attach_file)直接保存到某种文本列中。您需要先将其更改为字符串才能执行此操作-例如通过serialize()json_encode()


尝试稍微更改一下代码,

1
2
3
4
5
6
7
8
$form_content = array(
        'name'          => $request->get('name'),
        'description'   => $request->get('description'),
        'attach'        => $attach_file
    );


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

或者你可以做,

1
2
3
4
5
6
7
8
$form_content = array(
        'name'          => $request->get('name'),
        'description'   => $request->get('description'),
        'attach'        => $attach_file
    );


AddFile::create($form_content);