before i got this problem, i was try to connect the database from laravel to phpmyadmin and i think it’s solved i don’t know if it solved or not yet cause the error said different before it says "SQLSTATE[HY000] [1045] Access denied for user ‘homestead’@’localhost’ (using password: YES)" something like that and after i change a little codes and i got another problem.
i got an error messages like Undefined Variable: Key from this file name tabelstok.blade.php
codes from tabelstok.blade.php :
<!DOCTYPE html>
<html>
<head>
<title>Tabelstok</title>
<h1> Tabel Stok Master </h1>
</head>
<body>
<table width="80%" border="1">
<th> No </th>
<th> Kode Barang </th>
<th> Nama Barang </th>
<th> Harga Barang Terakhir </th>
<th> Id Satuan </th>
<?php
foreach ($ds as $key) {
}
?>
<tr>
<td> {{$key->no}} </td>
<td> {{$key->kode}} </td>
<td> {{$key->nama}} </td>
<td> {{$key->hbt}} </td>
<td> {{$key->ids}} </td>
</tr>
</table>
</body>
</html>
and this is my Controller codes :
<?php
namespace AppHttpControllers;
use IlluminateFoundationBusDispatchesJobs;
use IlluminateRoutingController as BaseController;
use IlluminateFoundationValidationValidatesRequests;
use IlluminateFoundationAuthAccessAuthorizesRequests;
use IlluminateSupportFacadesDB;
use IlluminateHttpRequest;
class regbcontroller extends BaseController
{
//class terdiri dari :
//1. fungsi/method + atribut/data/variabel
public function fungsimenuregb(){
return view ('regb.viewcontroller');
}
public function fungsimenuregb2(){
return view ('regb.viewcontroller2');
}
public function fungsimenuparam(Request $r){
$nil1=$r->nilai1;
$nil2=$r->nilai2;
return view ('regb.viewparam')
->with('nilaa1', $nil1)
->with('nilaa2', $nil2)
;
}
public function fungsitabelstok(Request $r){
$ds=DB::table('tbbarang')
->get();
return view ('regb.tabelstok')
->with('ds',$ds);
}
public function simpantambahstok(Request $r){
//proses simpan
$simpan=array
(
'kode' => $r -> kd,
'nama' => $r -> nama,
'hbt' => $r -> hbt,
'idsatuan' => $r -> ids
);
DB::table ('tbbarang') ->insertgetID($simpan);
return redirect('tabelstok')
-> with('pesan', 'profile updateSuccess');
}
}
and this is my Route codes :
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//UNTUK MEMANGGIL WELCOME
Route::get('/', function () {
return view('welcome');
});
//UNTUK MENERIMA REQUEST MENUSATU
Route::get('/menusatu', function () {
return view('regb.halamansatu');
});
Route::get('/menudua', function () {
return view('regb.halamandua');
});
Route::get('/menukontroller','regbcontroller@fungsimenuregb');
Route::get('/menukontroller2','regbcontroller@fungsimenuregb2');
Route::get('/menu/{nilai1}/{nilai2}','regbcontroller@fungsimenuparam');
Route::get('/tabelstok','regbcontroller@fungsitabelstok');
Route::get('/tambahstok','regbcontroller@formtambahstok');
Route::get('/editstok', 'regbcontroller@formeditstok');
Route::post('/simpantambahstok', 'regbcontroller@simpantambahstok');
Route::post('/simpaneditstok', 'regbcontroller@simpaneditstok');
i don’t understand. please help
2
Answers
I just found the solution from @lagbox's comment. I tried to put the whole
<tr>
to</tr>
inside the loop like he said and it works.You are not in foreach loop. Your
$key
variable is out of loop 😉