How to fix "Undefined variable: gambar (View: C:\xampp\htdocs\apmt\resources\views\upload.blade.php)

Viewed 9919

enter image description hereI can't understand why the variable is undefined for gambar at foreach. I'm also new to PHP Laravel 5.8.

(upload.blade.php)


<table class="table table-bordered table-striped">
    <thead>
        <tr>
            <th width="1%">File</th>
            <th>Keterangan</th>
            <th width="1%">OPSI</th>
        </tr>
    </thead>
    <tbody>

        @foreach ($gambar as $g)`here is the undefined variable`
          <tr>
              <td><img width="150px" src="{{ url('/data_file/'.$g->file) }}"></td>
              <td>{{$g->keterangan}}</td>
              <td><a class="btn btn-danger" href="/upload/hapus/{{ $g->id }}">HAPUS</a></td>
          </tr>
        @endforeach
    </tbody>
</table>

UploadController.php

    public function upload(){
        $gambar = Gambar::get();
        return view('upload',['gambar' => $gambar]);
    }   

    public function proses_upload(Request $request){
        $this->validate($request, [
            'file' => 'required|file|image|mimes:jpeg,png,jpg|max:2048',
            'keterangan' => 'required',
        ]);

        // menyimpan data file yang diupload ke variabel $file
        $file = $request->file('file');

        $nama_file = time()."_".$file->getClientOriginalName();

                // isi dengan nama folder tempat kemana file diupload
        $tujuan_upload = 'data_file';
        $file->move($tujuan_upload,$nama_file);

        Gambar::create([
            'file' => $nama_file,
            'keterangan' => $request->keterangan,
        ]);

        return redirect()->back();
    }

I expect that it will show an image from my file (data_file).The picture I upload all are just working fine. Just cannot show up in table.

4 Answers

You have missed passing the gambar in the proses_upload function and I guess you are inserting the image in the upload.blade.php

public function proses_upload(Request $request){
$this->validate($request, [
    'file' => 'required|file|image|mimes:jpeg,png,jpg|max:2048',
    'keterangan' => 'required',
]);

// menyimpan data file yang diupload ke variabel $file
$file = $request->file('file');

$nama_file = time()."_".$file->getClientOriginalName();

        // isi dengan nama folder tempat kemana file diupload
$tujuan_upload = 'data_file';
$file->move($tujuan_upload,$nama_file);

Gambar::create([
    'file' => $nama_file,
    'keterangan' => $request->keterangan,
]);
$gamber = Gambar::get();
return view('upload',['gambar' => $gambar]);

}

kindly please edit your post and put your controller code in the question. but in my opinion. the $gambar not passed correctly in your controller. the step might help :

  1. check in your controller, is $gambar has been passed correctly to your view.
  2. in laravel. you can use compact or with() method for passing the variables. the example in controller:

    $gambar = 'path/to/file/gambar.png'
    return view('index',compact('gambar')) // it will passed $gambar in index.blade
    

or

$gambar = 'path/to/file/gambar.png'
return view('index')->with('gambar',$gambar); // it's also passed $gambar in index.blade
public function upload()
{
$gambar = Gambar::get();
return view('upload',compact('gambar'));
OR
return viw('upload')->with('gambar',$gambar);
}   

public function proses_upload(Request $request){
$this->validate($request, [
    'file' => 'required|file|image|mimes:jpeg,png,jpg|max:2048',
    'keterangan' => 'required',
]);

// menyimpan data file yang diupload ke variabel $file
$file = $request->file('file');

$nama_file = time()."_".$file->getClientOriginalName();

        // isi dengan nama folder tempat kemana file diupload
$tujuan_upload = 'data_file';
$file->move($tujuan_upload,$nama_file);

Gambar::create([
    'file' => $nama_file,
    'keterangan' => $request->keterangan,
]);

return redirect()->route('upload-view');
} 

In web.php

  Route::get('upload',UploadController@upload)->name('upload-view');

In your view

  <table class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th width="1%">File</th>
                    <th>Keterangan</th>
                    <th width="1%">OPSI</th>
                </tr>
            </thead>
            <tbody>

                @foreach($gambar as $g)
                <tr>
                    <td><img width="150px" src="{{ url('/data_file/'.$g->file) }}"></td>
                    <td>{{$g->keterangan}}</td>
                    <td><a class="btn btn-danger" href="/upload/hapus/{{ $g->id }}">HAPUS</a></td>
                </tr>
            @endforeach
            </tbody>
        </table>

Please do this;

$gambar = Gambar::all();
return view('upload',compact('gambar'));

First return the variable in your controller, to check what values it returns, just like I did below, if it returns then use:

$gambar = Gambar::all();
return $gambar;
Related