我不明白为什么在foreach
循环中gambar
变量未定义。我也是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();
}
我希望它能从我的文件(data_file
)中显示一张图片。我上传的所有图片都可以正常工作,只是不能在表格中显示。
$gambar
变量并传递给指定视图。 - zlatanredirect()->route('named-route')
来替代redirect()->back()
吗? - Jithesh Jose