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...

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

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备