I am creating a Laravel crud. in here i have a DB table called:
File:
‘title’,’description_short’,’description_long,’file’,’language’
the problem lays in the ‘file’ column. here i can upload files like word and excel. but whenever i edit a row with a file attached. the file gets removed if i don’t upload A or THE file again.
edit.blade:
<div class="row">
<div class="col-sm-8 offset-sm-2">
<h1 class="display-3"> {{('Editing files')}}</h1>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
<br />
@endif
@if(empty($fileEdit))
<div>{{('Choose file to edit')}}</div>
@else
<form method="post" action="{{ route('admin.file.update', $fileEdit->id) }}">
@method('PUT')
@csrf
<div class="form-group">
<label for="name">{{('title')}}</label>
<input type="text" class="form-control" name="title" value="{{ $fileEdit->title }}" />
</div>
<div class="form-group">
<label for="name"> {{('Short description')}}</label>
<input type="text" class="form-control" name="description_short" value="{{ $fileEdit->description_short }}" />
</div>
<div class="form-group">
<label for="name"> {{('Long description')}}</label>
<input type="text" class="form-control" name="description_long" value="{{ $fileEdit->description_long }}" />
</div>
<div class="form-group">
<label for="name"> {{('file')}}</label>
<input type="file" class="form-control" name="file" value="{{ $fileEdit->file }}" />
</div>
<div class="form-group">
<label for="name">{{('language')}}</label>
<select name="language_id" class="form-control">
@foreach($languages as $language)
<option value=" {{$language->id}}">{{$language->name}}</option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
@endif
</div>
</div>
controller:
<?php
namespace AppHttpControllersadmin;
use AppHttpControllersController;
use IlluminateHttpRequest;
use AppModelsFile;
use AppModelsLanguage;
class FileController extends Controller
{
public function index()
{
$files = File::with('language')->get();
$languages = Language::all();
return view('admin.file.index', compact('files', 'languages'));
}
public function create()
{
//
}
public function store(Request $request)
{
$request->validate([
'title'=>'required',
'description_short'=>'',
'description_long'=>'',
'file'=>'',
'language_id'=> [
'required', 'exists:language,id'
],
]);
$file = new File([
'title'=> $request->get('title'),
'description_short'=> $request->get('description_short'),
'description_long'=> $request->get('description_long'),
'file'=>$request->get('file'),
'language_id'=> $request->language_id,
]);
$file->save();
return back();
}
public function show($id)
{
//
}
public function edit($id)
{
$files = File::all();
$fileEdit = File::find($id);
$languages = Language::all();
return view('admin.file.index', compact('files', 'fileEdit', 'languages'));
}
public function update(Request $request, $id)
{
$request->validate([
'title'=>'required',
'description_short'=>'',
'description_long'=>'',
'file'=>'',
'language_id'=> [
'required', 'exists:language,id'
],
]);
$fileData = [
'title'=> $request->title,
'description_short'=> $request->description_short,
'description_long'=> $request->description_long,
'file'=>$request->file,
'language_id'=> $request->language_id,
];
File::whereId($id)->update($fileData);
return redirect()->route('admin.file.index');
}
public function destroy($id)
{
$file = File::find($id);
$file->delete();
return redirect()->route('admin.file.index');
}
}
File model:
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class File extends Model
{
use HasFactory;
public function language(){
return $this->belongsTo(Language::class);
}
protected $table = 'file';
protected $fillable = [
'title',
'description_short',
'description_long',
'file',
'language_id',
'user_id',
];
}
3
Answers
if you leave the validation field blank it may take the input as empty there for use
bail
which will not validation but the input should be empty. you will update with a data you are passing to validator.make sure you have added
file
on fillable properties onFile model
for security reasons, you can’t set value of input type file. so
is not adding the old file in the input. what you can do is checking if user added any file in controller.
in controller
Try this