Reputation: 1
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');
}```
Upvotes: 0
Views: 56
Reputation: 13224
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;
Upvotes: 1