Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel Edit removes uploaded file

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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 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',
    ];
}

>Solution :

for security reasons, you can’t set value of input type file. so

<input type="file" class="form-control" 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" class="form-control" 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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading