doufen3838 2017-05-01 23:53
浏览 41
已采纳

在Laravel我正在尝试更新图像并且它可以工作,但是当我决定不添加新文件时,我收到此错误:一般错误:1364 [重复]

This question already has an answer here:

I'm trying to update a table and it works perfectly but when I try to update whiteout changing the file it fails and gives me this error.

The error:

SQLSTATE[HY000]: General error: 1364 Field 'poster' doesn't have a default value (SQL: insert into patrocinadores (nombre, link, categoria, updated_at, created_at) values (Patrocinadores, http://127.0.0.1:8000/patrocinadores/creates, 3, 2017-05-01 23:44:44, 2017-05-01 23:44:44))

Here is my controller store()

public function store(Request $request){

    $this->validate(request(), [

        'nombre' => 'required',
        'link' => 'required',
        'poster' => 'image|image_size:<=1000',
        'categoria' =>'required',
    ]);

    $patrocinadores = new Patrocinadores;

    $patrocinadores->nombre = request('nombre');
    $patrocinadores->link = request('link');
    $newFoto=request()->file('poster');
    if($newFoto){
      $name=$newFoto. '.' . $newFoto->getClientOriginalExtension();
      $patrocinadores->poster = $newFoto->move('./uploads/', $name);
    }
    $patrocinadores->categoria = request('categoria');

    $patrocinadores->save();


    return redirect('/patrocinadores'); }

HTML:

  <div class="form-control-file">
    <label for="poster">Póster</label>

      <input type="file" class="form-control-file" id="poster" name="poster" aria-describedby="fileHelp"  accept="image/*" >

      <br>
      <div id="preview"><img src="{{asset($patrocinadores->poster)}}" ></div>


    </div>
</div>
  • 写回答

1条回答 默认 最新

  • duancutan4770 2017-05-02 01:38
    关注

    The only place you set the poster field is in the if statement... so if $request->file() returns null it will never get set... probably what happens...

      $newFoto=request()->file('poster');
        if($newFoto){
          $name=$newFoto. '.' . $newFoto->getClientOriginalExtension();
          $patrocinadores->poster = $newFoto->move('./uploads/', $name);
        }
    

    Now $newFoto is an instance of Illuminate\Http\UploadedFile, the move() method on it returns a file object...

    You could take advantage of the storeAs() method and store the path of the file in the database... like so:

    $patrocinadores->poster = $request->poster->storeAs('uploads', $name);
    

    Note that this will store it in your storage directory under uploads (/storage/uploads) which I prefer over root... now even better... you can check for file upload success, the user could abort on you...

        if ($request->file('poster')->isValid()) {
            $patrocinadores->poster = $request->poster->storeAs('uploads', $name);
        }
        else{
            $patrocinadores->poster = 'path to default photo or avatar';
        }
    

    Hope this helps...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题