我不明白为什么gambar
上foreach
的变量未定义。我也是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
)中的图像。我上传的所有图片都工作正常。只是无法显示在表格中。
答案 0 :(得分:1)
您错过了在gambar
函数中传递proses_upload
的情况,我想您正在将图像插入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]);
}
答案 1 :(得分:0)
请编辑您的帖子,并在问题中添加控制器代码。 但我认为。 $ gambar未在您的控制器中正确传递。 该步骤可能会有所帮助:
。您可以使用compact
或with()
方法来传递变量。
控制器中的示例:
$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
答案 2 :(得分:0)
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>
答案 3 :(得分:-1)
请执行此操作
$gambar = Gambar::all();
return view('upload',compact('gambar'));
首先在控制器中返回该变量,以检查其返回的值,就像我在下面所做的那样,如果返回,则使用:
$gambar = Gambar::all();
return $gambar;