dpzp5127 2019-06-17 08:28
浏览 81

现有图像未以编辑形式显示以在laravel中更新(5.8)

I am trying to make todo list wherein updating form other field value is shown but for image the value is read from the folder but not display in the input field.

everything works fine else this problem.

This display the image name<?php echo $task->img; ?>

I use dd("$task->img"); to check the values.

edit.blade.php

        <label for="title">Task Title</label>
        <input type="text" value="{{$task->title}}" class="form-control" id="taskTitle"  name="title" >
      </div>
      <div class="form-group col-6">
        <label for="description">Task Description</label>
        <input type="text" value="{{$task->description}}" class="form-control" id="taskDescription" name="description" >
      </div>
 <div class="form-group col-6">
        <label for="img">Task Image</label>
        <input type="file" value="{!! $task->img !!}" class="form-control" id="taskImg" name="img" >
      </div>

taskcontroller.php

 public function update(Request $request, Task $task)
    {

        // dd("$task");
        //Validate
        $request->validate([
            'title' => 'required|min:3',
            'description' => 'required',
            'img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',

        ]);

        $task->title = $request->title;
        $task->description = $request->description;

        if($request->hasFile('img')) {
            $file = $request->file('img');
            $newVar = strtotime(date('d-m-Y h:i:s'));
            $extension = $file->getClientOriginalExtension(); // getting image extension
            $filename = $newVar.'.'.$extension;
            $file->move('task',$filename);
            $task->img = $filename;
        }


        $task->update();
        $request->session()->flash('message', 'Successfully modified the task!');
       return redirect()->route('tasks.index');
    }```
  • 写回答

1条回答 默认 最新

  • dtypj3308 2019-06-18 08:10
    关注

    Adding a file as a default value is not gonna work for an input field. You probably want to add an extra div above the input field like:

    <div>
    <p>Current image</p>
    <img src="{!! asset($task->img) !!}">
    <p>Upload new image:</p>
     <div class="form-group col-6">
            <label for="img">Task Image</label>
            <input type="file" class="form-control" id="taskImg" name="img" >
          </div>
    </div>
    

    In your controller, you should then delete the old image if a new has been uploaded. I also added the asset(), helper. But you should use the location of your file because in your controller you don't seem to store it on a filesystem-disk.

    PS: You should also read this to simplify the upload code:

    https://laravel.com/docs/5.8/filesystem#storing-files For example, this replaces all those lines between the if statement

    $path = $request->file('img')->store('images');
    $task->img = $path;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog