Laravel CRUD Operation with File Upload

Laravel CRUD Operation with File Upload

1. Set up Laravel Project & Database

laravel new file_upload_crud cd file_upload_crud

Update your .env file with your DB credentials:

DB_DATABASE=file_upload_crud DB_USERNAME=root DB_PASSWORD=

Run migrations:

php artisan migrate

2. Create Model, Migration, and Controller

php artisan make:model Document -mcr

Update the migration file database/migrations/xxxx_xx_xx_create_documents_table.php:

public function up() { Schema::create('documents', function (Blueprint $table) { $table->id(); $table->string('title'); $table->string('file_path'); $table->timestamps(); }); }

Then migrate:

php artisan migrate

3. Define Fillable Fields in Model

In app/Models/Document.php:

protected $fillable = ['title', 'file_path'];

4. Set Up Routes

In routes/web.php:

use App\Http\Controllers\DocumentController; Route::resource('documents', DocumentController::class);

5. Create Form UI for File Upload

In resources/views/documents/create.blade.php:

<form action="{{ route('documents.store') }}" method="POST" enctype="multipart/form-data"> @csrf <label>Title</label> <input type="text" name="title"> <label>Upload File</label> <input type="file" name="file_path"> <button type="submit">Submit</button> </form>

6. Store Logic in Controller

In app/Http/Controllers/DocumentController.php:

public function store(Request $request) { $request->validate([ 'title' => 'required', 'file_path' => 'required|file|mimes:jpg,jpeg,png,pdf|max:2048', ]); if ($request->hasFile('file_path')) { $fileName = time().'_'.$request->file('file_path')->getClientOriginalName(); $path = $request->file('file_path')->storeAs('uploads', $fileName, 'public'); Document::create([ 'title' => $request->title, 'file_path' => $path, ]); } return redirect()->route('documents.index')->with('success', 'Document uploaded successfully.'); }

7. Show All Documents

In resources/views/documents/index.blade.php:

@foreach($documents as $doc) <p>{{ $doc->title }}</p> <a href="{{ asset('storage/' . $doc->file_path) }}" target="_blank">View File</a> <form action="{{ route('documents.destroy', $doc->id) }}" method="POST"> @csrf @method('DELETE') <button type="submit">Delete</button> </form> @endforeach

In Controller:

public function index() { $documents = Document::latest()->get(); return view('documents.index', compact('documents')); }

8. Edit & Update Logic

In resources/views/documents/edit.blade.php:

<form action="{{ route('documents.update', $document->id) }}" method="POST" enctype="multipart/form-data"> @csrf @method('PUT') <input type="text" name="title" value="{{ $document->title }}"> <input type="file" name="file_path"> <button type="submit">Update</button> </form>

In Controller:

public function update(Request $request, Document $document) { $request->validate([ 'title' => 'required', 'file_path' => 'nullable|file|mimes:jpg,jpeg,png,pdf|max:2048', ]); $data = ['title' => $request->title]; if ($request->hasFile('file_path')) { $fileName = time().'_'.$request->file('file_path')->getClientOriginalName(); $path = $request->file('file_path')->storeAs('uploads', $fileName, 'public'); $data['file_path'] = $path; } $document->update($data); return redirect()->route('documents.index')->with('success', 'Document updated successfully.'); }

9. Delete Document

In Controller:

public function destroy(Document $document) { if ($document->file_path && \Storage::disk('public')->exists($document->file_path)) { \Storage::disk('public')->delete($document->file_path); } $document->delete(); return redirect()->route('documents.index')->with('success', 'Document deleted successfully.'); }

10. Link Storage Folder

Run:

php artisan storage:link

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close