Привет, я пытаюсь редактировать файлы в Laravel, но когда я редактирую файл, его имя меняется в соответствии с редактированием в БД, но файл не заменяет предыдущее в папке с файлами, m использует для этого маршрут ресурса, ,,,,,,,,,,,,,, Примечание: и когда я сохраняю файл, то его имя сохраняется в одной таблице, а его данные хранятся в другой таблице, поэтому необходимо, чтобы при редактировании файла его данные из другой файл должен измениться, как это сделать?
Контроллер:
<?php
namespace App\Http\Controllers;
use App\File;
use Illuminate\Http\Request;
use Spatie\PdfToText\Pdf;
use Illuminate\Support\Facades\File as FileFacade;
use DB;
class FileController extends Controller
{
public function index()
{
$data = File::latest()->paginate(5);
return view('index', compact('data'))->with('i', (request()->input('page', 1) - 1) * 5);
}
public function create()
{
return view('create');
}
public function store(Request $request)
{
request()->validate([
'filename' => 'required',
]);
$cols = ['Battery', 'No_of_questions_attempted', 'SAS', 'NPR', 'ST', 'GR'];
$files = $request->file('filename');
foreach ($files as $file) {
$file_id = File::create([
'filename' => $file->getClientOriginalName(),
])->id;
$text = Pdf::getText($file->getRealPath(), base_path('poppler-utils/bin/pdftotext.exe'));
$page5 = strpos($text, 'Page 5 of 8');
$start = strpos($text, 'Verbal', $page5);
$end = strpos($text, "\r\n\r\nSAS (with 90% confidence bands)", $page5);
$table = substr($text, $start, $end - $start);
$data = array_map(function ($row) use ($cols, $file_id) {
return array_merge(compact('file_id'), array_combine($cols, $row));
}, collect(explode("\r\n\r\n", $table))->chunk(count($cols))->toArray());
\DB::table('importpdfs')->insert($data);
$file->move(public_path('files'), $file->getClientOriginalName());
}
return redirect('/file')->with('Success', 'File uploaded successfully');
}
public function show($id)
{
$data = File::findOrFail($id);
//dd($data);
return view('show', compact('data'));
}
public function edit($id)
{
$data = File::findOrFail($id);
$files = \DB::table('files')->get();
return view('edit', compact('data', 'files'));
}
public function update(Request $request, $id)
{
// Do the validation before you do anything else
$request->validate([
'filename' => 'required',
]);
// Get the file from the request
$file = $request->file('filename');
$clientName = $file->getClientOriginalName();
// Store the file in the `files` directory
$path = $file->move(public_path('files'), $clientName);
// Update the model
File::whereId($id)->update(['filename' => $clientName]);
// Redirect to some page
return redirect('file')->with('success', 'Data is successfully updated');
}
public function destroy($id)
{
$file = File::find($id);
$file->delete(public_path('files'));
$file->delete();
return redirect('file')->with('success', 'Data is successfully deleted');
}
}
файл лезвия:
<form method="post" action="{{ route('file.update', $data->id) }}" enctype="multipart/form-data">
@csrf
@method('PATCH')
<div class="form-group">
<label for="EditFile">Edit File</label>
<input type="file" name="filename" class="form-control-file">
<img src="{{ URL::to('/') }}/files/{{ $data->file }}" width="100"/>
<img src="./images/pdf.png" class="img-circle elevation-2" alt="Pdf Image" width="50">
<input type="hidden" name="hidden_file" value="{{ $data->file }}" />
</div>
<br>
<input type="submit" name="edit" class="btn btn-primary input-lg" value="Edit" />
</form>