I have been stuck with this error about 3 hours either with error message Undefined variable $certificates
or Call to a member function isEmpty() on array
or any other errors. Actually, I wanna display certificate data belongs to user that logs in instead all data. But I got frustated, thats why I wanna display all data.
Here’s my SaveController.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsCertificate;
use CarbonCarbon;
use IlluminateSupportFacadesAuth;
class SaveController extends Controller
{
function store(Request $r)
{
$now = Carbon::now();
$format = $now->format('D j-M-Y g:i:s.u A');
$user = Auth::user();
$certificate = new Certificate();
$certificate->user_id = $user->id;
$certificate->name = $r->input('name');
$certificate->sha512 = $r->input('sha512');
$certificate->time = $format;
$certificate->save();
// $certificates = Certificate::where('user_id', $user->id)->orderBy('time')->get();
$certificates = Certificate::all();
return redirect('/')->with(compact('certificates'));
}
}
and this is my index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
<script src="{{ asset('js/app.js') }}"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
<title>Non-Academic Digital Certificate Validator (SHA-512)</title>
</head>
<body>
@guest
<header>
<h1 id="head1">Non-Academic Digital Certificate Validator (SHA-512)</h1>
<div class="auth">
<a href="/login"><button>Login</button></a>
<a href="/register"><button>Register</button></a>
</div>
</header>
<div class="input">
<div id="droppable-zone">
<div id="droppable-zone-wrapper">
<div id="droppable-zone-text">Drag & drop your certificate here OR click to browse</div>
</div>
<input class="droppable-file" id="input" type="file" accept="image/jpeg, image/png, application/pdf"
onchange="hash()">
</div>
</div>
<div class="output">
<textarea id="output" placeholder="SHA-512 Checksum" readonly></textarea>
</div>
<div class="remove">
<button id="remove" onclick="clearInput()">Remove</button>
</div>
@endguest
@auth
<header>
<h1 id="head1">Non-Academic Digital Certificate Validator (SHA-512)</h1>
<div class="auth">
<form class="search">
<input type="text" placeholder="Search...">
</form>
<form action="{{ route('logout') }}" method="POST">
@csrf
<button>Logout</button>
</form>
</div>
</header>
<div class="input">
<div id="droppable-zone">
<div id="droppable-zone-wrapper">
<div id="droppable-zone-text">Drag & drop your certificate here OR click to browse</div>
</div>
<input class="droppable-file" id="input" type="file" accept="image/jpeg, image/png, application/pdf" onchange="hash()">
</div>
</div>
<div class="output">
<textarea id="output" placeholder="SHA-512 Checksum" readonly></textarea>
</div>
<div class="remove">
<button id="remove" onclick="clearInput()">Remove</button>
</div>
<div class="save">
<form action="{{ route('save') }}" method="POST">
@csrf
<input type="hidden" name="name" id="namefile" readonly>
<input type="hidden" name="sha512" id="hash" readonly>
<button id="save" type="submit" disabled>Save</button>
</form>
</div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>SHA-512</th>
</tr>
</thead>
<tbody>
{{-- @if ($certificates->isEmpty())
<tr>
<td colspan="3">No Data</td>
</tr>
@else --}}
@foreach ($certificates as $c)
<tr>
<td>{{ $c->name }}</td>
<td>{{ $c->time }}</td>
<td>{{ $c->sha512 }}</td>
</tr>
@endforeach
{{-- @endif --}}
</tbody>
</table>
@endauth
</body>
</html>
this is my Routes web.php
<?php
use AppHttpControllersRegisterController;
use AppHttpControllersLoginController;
use AppHttpControllersSaveController;
use IlluminateSupportFacadesRoute;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
return view('index');
});
Route::get('/register', [RegisterController::class, 'index']);
Route::post('/register', [RegisterController::class, 'store']);
Route::get('/login', [LoginController::class, 'index']);
Route::post('/login', [LoginController::class, 'auth']);
Route::post('/save', [SaveController::class, 'store'])->name('save');
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');
I have define all classes on web.php too.
Ive tried modified those 2 file but theres nothing work, only return error message. Could someone help me? thx in advance
2
Answers
with function will set the certificates to session data, so to access certificates you need to check if session has certificates
Check the documentation here
Better approach will following the standard
Rename the SaveController to CertificateController this will be more
clear
Use Request Validation to validate your request before inserting to database Laravel Validation
Change Your route to
Route::post('certificate',[CertificateController::class,'store']);
within your store method of CertificateController Redirect to base
route
change your base route ‘/’ to call a controller method maybe like
HomeController index function
Route::get('/',[HomeController::class,'index']);
in HomeController index get all certificates and return it the view
now in view you could access the $certificates as you did in your
code
You are trying to get data on index page for that you need to change this
return redirect(‘/’)->with(compact(‘certificates’));
to
return view(‘index’)->with(compact(‘certificates’));
I hope this will help you. Thanks