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 >
<div >
<h1 > {{('Editing files')}}</h1>
@if ($errors->any())
<div >
<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 >
<label for="name">{{('title')}}</label>
<input type="text" name="title" value="{{ $fileEdit->title }}" />
</div>
<div >
<label for="name"> {{('Short description')}}</label>
<input type="text" name="description_short" value="{{ $fileEdit->description_short }}" />
</div>
<div >
<label for="name"> {{('Long description')}}</label>
<input type="text" name="description_long" value="{{ $fileEdit->description_long }}" />
</div>
<div >
<label for="name"> {{('file')}}</label>
<input type="file" name="file" value="{{ $fileEdit->file }}" />
</div>
<div >
<label for="name">{{('language')}}</label>
<select name="language_id" >
@foreach($languages as $language)
<option value=" {{$language->id}}">{{$language->name}}</option>
@endforeach
</select>
</div>
<button type="submit" >Update</button>
</form>
@endif
</div>
</div>
controller:
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\File;
use App\Models\Language;
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 App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
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',
];
}
CodePudding user response:
for security reasons, you can't set value of input type file. so
<input type="file" name="file" value="{{ $fileEdit->file }}" />
is not adding the old file in the input. what you can do is checking if user added any file in controller.
<input type="file" name="file" />
in controller
$fileData = [
'title' => $request->title,
'description_short' => $request->description_short,
'description_long' => $request->description_long,
'language_id' => $request->language_id,
];
if ($request->get('file')) {
$fileData['file'] = $request->file;
}
File::whereId($id)->update($fileData);
CodePudding user response:
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.
$request->validate([
'title'=>'required',
'description_short'=>'bail',
'description_long'=>'bail',
'file'=>'bail',
'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);
make sure you have added file
on fillable properties on File model
CodePudding user response:
Try this
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,
'language_id'=> $request->language_id,
];
if (isset ($request->file)) {
$fileData['file'] = $request->file
}
File::whereId($id)->update($fileData);
return redirect()->route('admin.file.index');
}