After successfully login from auth, I wan to show the data from database. But I got an error like this.
Undefined variable: barangs (View: C:xampp7htdocsexample-appresourcesviewsbarang.blade.php) in c1126675c0e1af0f5c253b69d8a8d64d968d987a.php line 188
and this is the login controller
class loginController extends Controller { function auth(Request $req){ $username = $req->input('your_name'); $password = md5($req->input('your_pass')); $chekLogin = DB::table('user')->where(['username'=>$username,'password'=>$password])->get(); if(count($chekLogin) > 0 ){ return view('barang'); } else { echo " Login Failed, Wrong Data"; } ; } }
and this is my barangController
class barangController extends Controller { public function data(){ $barangs = DB::table('barang')->get(); //return $barangs; return view('barang',['barangs'=>$barangs]); } }
this is my barang.blade.php
<div class="x_content"> <p class="text-muted font-13 m-b-30"> Info Barang </p> <table id="datatable" class="table table-striped table-bordered"> <thead> <tr> <th>Kode Barang</th> <th>Nama Barang</th> <th>Action</th> </tr> </thead> <tbody> @foreach ($barangs as $item) <tr> <td>{{$item->kode_barang}}</td> <td>{{$item->nama_barang}}</td> <td><a href="" class="btn btn-primary btn-sm"><i class="fa fa-pencil"></i></a> </td> </tr> @endforeach </tbody> </table> </div> </div> </div>
and this is my route
Route::get('/', function () { return view('index'); }); Route::post('auth','loginController@auth')->name('login.auth'); Route::get('databarang', 'barangController@data');
please help me,I dont know why barang is not defined in here, may I forgot to add something? Thankyou before
Advertisement
Answer
The error is because you’re not including the barangs
data when you return your view
in the auth
function.
Instead of returning a view
from your auth
, redirect the user
:
if(count($chekLogin) > 0 ){ return redirect(url('databarang')); }
What this will do is redirect your user
to the route which then returns your view('barang')
but includes the missing barangs
data.