Create endpoint for uploading attachments
ADD
app/Http/Controllers/AttachmentController.php
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Repositories\AttachmentRepository;
+use Illuminate\Http\Request;
+
+class AttachmentController extends Controller
+{
+ private $attachmentRepository;
+
+ public function __construct(AttachmentRepository $attachmentRepository)
+ {
+ $this->attachmentRepository = $attachmentRepository;
+ }
+
+ public function store(Request $request)
+ {
+ $request->validate([
+ 'transaction_type' => 'required|in:earning,spending',
+ 'transaction_id' => 'required',
+ 'file' => 'required|mimes:jpeg,png,pdf'
+ ]);
+
+ $transactionType = $request->transaction_type;
+ $transactionId = $request->transaction_id;
+
+ $this->attachmentRepository->create($transactionType, $transactionId);
+
+ return redirect('/' . $transactionType . 's/' . $transactionId);
+ }
+}

MODIFY
app/Models/Attachment.php
app/Models/Attachment.php
@@ -8,4 +8,10 @@ use Illuminate\Database\Eloquent\SoftDeletes;
class Attachment extends Model
{
use SoftDeletes;
+
+ protected $fillable = [
+ 'transaction_type',
+ 'transaction_id',
+ 'file_path'
+ ];
}

ADD
app/Repositories/AttachmentRepository.php
+<?php
+
+namespace App\Repositories;
+
+use App\Models\Attachment;
+use Exception;
+
+class AttachmentRepository
+{
+ public function create(string $transactionType, int $transactionId): Attachment
+ {
+ if ($transactionType !== 'earning' && $transactionType !== 'spending') {
+ throw new Exception('Invalid transaction type ("' . $transactionType . '")');
+ }
+
+ return Attachment::create([
+ 'transaction_type' => $transactionType,
+ 'transaction_id' => $transactionId,
+ 'file_path' => 'https://via.placeholder.com/500'
+ ]);
+ }
+}

MODIFY
routes/web.php
routes/web.php
@@ -16,6 +16,8 @@ Route::post('/register', 'RegisterController@store');
Route::group(['middleware' => ['auth']], function () {
Route::get('/dashboard', 'DashboardController')->name('dashboard');
+ Route::post('/attachments', 'AttachmentController@store');
+
Route::get('/transactions/create', 'TransactionController@create')->name('transactions.create');
Route::name('transactions.')->group(function () {

Create endpoint for uploading attachments