繁体   English   中英

如何修复“未定义变量:gambar(查看:C:\xampp\htdocs\apmt\resources\views\upload.blade.php)

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

在此处输入图像描述 我不明白为什么foreachgambar变量未定义。 我也是 PHP Laravel 5.8 的新手。

(上传.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>

上传控制器.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();
    }

我希望它会显示我的文件( data_file )中的图像。我上传的所有图片都可以正常工作。 只是不能出现在表中。

您错过了在proses_upload gambar中通过gambar,我猜您将图像插入到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]);

}

请编辑您的帖子并将您的 controller 代码放入问题中。 但在我看来。 $gambar 未在 controller 中正确传递。 该步骤可能会有所帮助:

  1. 检查您的 controller,$gambar 是否已正确传递到您的视图。
  2. 在 laravel 中。 您可以使用compactwith()方法来传递变量。 controller 中的示例:

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

或者

$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');
} 

web.php

  Route::get('upload',UploadController@upload)->name('upload-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>

请这样做;

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

首先返回 controller 中的变量,检查它返回的值,就像我在下面所做的那样,如果它返回然后使用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM