Feb 28, 2024
0
Level 1
Laravel 9 in Hostinger can't fetch image after it's up on domain
Im having a problem for a whole week now and i can't seem to fetch the image that user uploaded in their application ( loaning web application ) and i test it on localhost, it's working but when in the hostinger side it always says 404 or can't find when the admin going to check their signature picture.
I tried to look up on the internet regarding on these type of error, i tried everything but nothing is working.
This is how i handle the backend and view. LoanProcessingController:
public function application(Request $request, $loanReference)
{
$currentLoanApplication = LoanApplication::with('approvals')->where('loan_reference', $loanReference)->firstOrFail();
$bookKeeperApproved = $currentLoanApplication->approvals->contains(function($approval) {
return $approval->book_keeper == true;
}) && $currentLoanApplication->application_status !== 'reject';
$sortColumn = $request->query('sort', 'application_date');
$sortDirection = $request->query('direction', 'desc');
$query = LoanApplication::where('account_number_id', $currentLoanApplication->account_number_id);
if ($sortColumn == 'application_status') {
switch ($sortDirection) {
case 'pending':
$query->orderByRaw("FIELD(application_status, 'pending', 'approved', 'rejected')");
break;
case 'approved':
$query->orderByRaw("FIELD(application_status, 'approved', 'rejected', 'pending')");
break;
case 'rejected':
$query->orderByRaw("FIELD(application_status, 'rejected', 'pending', 'approved')");
break;
default:
$query->orderBy('application_status', 'asc');
break;
}
} else {
$direction = in_array(strtolower($sortDirection), ['asc', 'desc']) ? $sortDirection : 'desc';
$query->orderBy($sortColumn, $direction);
}
$pastLoanApplications = $query->paginate(5);
try {
$mediaItems = $currentLoanApplication->mediaItems()->where('loan_id', $currentLoanApplication->id)->get();
} catch (\Exception $e) {
Log::error("Error fetching media items: " . $e->getMessage());
}
if ($request->ajax()) {
$tbodyHtml = view('admin.loans.partials.loan_table_info', ['pastLoanApplications' => $pastLoanApplications])->render();
return response()->json([
'table' => $tbodyHtml,
]);
}
return view('admin.loans.application', compact('currentLoanApplication', 'pastLoanApplications', 'mediaItems', 'bookKeeperApproved'));
}
This is my media Model:
use HasFactory;
public $table = 'media';
protected $fillable = ['signature', 'take_home_pay', 'loan_id'];
public function loanApplication()
{
return $this->belongsTo(LoanApplication::class, 'loan_id');
}
This is my view:
@php
$signature = $currentLoanApplication->mediaItems->pluck('signature')->filter()->first();
$takeHomePay = $currentLoanApplication->mediaItems->pluck('take_home_pay')->filter()->first();
@endphp
<!-- Modal -->
<div class="modal fade" id="applicant_sign" tabindex="-1" aria-labelledby="#applicant_signModal" aria-hidden="true">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="applicant_signModal">Signature picture</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
@if($signature)
<img src="{{ asset($signature) }}" alt="Signature">
@endif
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- End of Modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#takehome_receipt">
Take homepay picture
</button>
<!-- Modal -->
<div class="modal fade" id="takehome_receipt" tabindex="-1" aria-labelledby="takehome_receiptLabel" aria-hidden="true">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="takehome_receipt">Take home pay Receipt</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
@if($takeHomePay)
<img src="{{ asset('public/' . $takeHomePay) }}" alt="Take Home Pay Document">
@endif
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
LoanApplyController:
$media = new Media();
// Handle signature
if ($request->hasFile('signature')) {
$signature = $request->file('signature');
$signatureName = time() . '_' . $signature->getClientOriginalName();
$signaturePath = 'signatures/' . $signatureName; // Corrected path
$signature->move(public_path('signatures'), $signatureName);
$media->signature = $signaturePath;
}
// Handle homepay_receipt
if ($request->hasFile('homepay_receipt')) {
$receipt = $request->file('homepay_receipt');
$receiptName = time() . '_' . $receipt->getClientOriginalName();
$receiptPath = 'receipts/' . $receiptName; // Corrected path
$receipt->move(public_path('receipts'), $receiptName);
$media->take_home_pay = $receiptPath;
}
// Assuming you're associating the media with the loan application
$media->loan_id = $application->id;
$media->save();
pls help me :((
Please or to participate in this conversation.