【问题标题】:Laravel SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into `add_file` (`0`)Laravel SQLSTATE [42S22]:找不到列:1054 '字段列表'中的未知列'0'(SQL:插入到`add_file`(`0`)
【发布时间】:2017-03-29 06:05:33
【问题描述】:

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

SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列 '0'(SQL:插入到 add_file (0) 值({"name":"Opeyemi Adam","description ":"谢谢兄弟","attach":[["CRITICAL TECHNICAL OBSERVATIONS.doc"]]})))

下面是模型

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

控制器

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


}

不知道字段列表来自哪里

【问题讨论】:

  • 删除了 SQL Server 标签,因为错误输出来自 MySQL。

标签: php mysql laravel laravel-5


【解决方案1】:

试着稍微改变你的代码,

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


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

或者你可以这样做,

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


AddFile::create($form_content);

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2015-01-19
      • 2021-03-29
      • 2021-01-03
      • 2018-02-21
      相关资源
      最近更新 更多