我正在使用 Laravel 5.4,我想从我的视图页面查看数据库中的数据 (
listpetani.blade.php
)。
这是我的项目代码:
HTML:
<div class="table-responsive">
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th><strong>No</strong></th>
<th><strong>Nama Petani</strong></th>
<th><strong>Alamat</strong></th>
<th><strong>No. Handphone</strong></th>
<th><strong>Lokasi</strong></th>
</tr>
</thead>
<tbody>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</tbody>
</table>
</div>
PHP: 在我的
listpetani.blade.php
中,我有一个空表,我想显示来自 的数据
Route::get('listpetani', function () {
$petani = DB::table('tbl_user')->pluck('id_user', 'username', 'alamat', 'no_telp', 'id_lokasi');
return view('listpetani', ['petani' => $petani]);
});
我页面上的表格:
我想在 Laravel 5.4 的视图中显示数据库中的所有数据。有人可以帮助我吗?
[解决]
谢谢大家,我已经解决这个问题了
这是已解决的代码
web.php(路线)
Route::get('listpetani', function () {
$petani = DB::table('tbl_user')->get();
return view('listpetani', ['petani' => $petani]);
});
在我的
listpetani.blade.php
@foreach($petani as $key => $data)
<tr>
<th>{{$data->id_user}}</th>
<th>{{$data->nama_user}}</th>
<th>{{$data->alamat}}</th>
<th>{{$data->no_telp}}</th>
<th>{{$data->id_lokasi}}</th>
</tr>
@endforeach
您也可以在视图中从数据库获取数据
@php( $contacts = \App\Contact::all() )
@php( $owners = \App\User::all())
<select class="form-control" name="owner_name" id="owner_name">
@foreach($contacts as $contact)
<option value="{{ $contact->contact_owner_id }}">{{ $contact->contact_owner }}</option>
@endforeach
</select>
如果将数据从控制器传递到视图会更好。
return view('greetings', ['name' => 'Victoria']);
查看文档: https://laravel.com/docs/8.x/views#passing-data-to-views
或者你可以在 Laravel Blade 中使用 @forelse 循环
@forelse($name as $data)
<tr>
<th>{{ $data->id}}</th>
<th>{{ $data->name}}</th>
<th>{{ $data->age}}</th>
<th>{{ $data->address}}</th>
</tr>
@empty
<tr><td colspan="4">No record found</td></tr>
@endforelse
在您的控制器中:
$select = DB::select('select * from student');
return view ('index')->with('name',$select);
您认为:
@foreach($name as $data)
<tr>
<th>{{ $data->id}}</th> <br>
<th>{{ $data->name}}</th> <br>
<th>{{ $data->age}}</th> <br>
<th>{{ $data->address}}</th>
</tr>
@endforeach
我希望这可以帮助你。
@foreach($petani as $p)
<tr>
<td>{{ $p['id_user'] }}</td>
<td>{{ $p['username'] }}</td>
<td>{{ $p['alamat'] }}</td>
<td>{{ $p['no_telp'] }}</td>
<td>{{ $p['id_lokasi'] }}</td>
</tr>
@endforeach
**在侧面控制器中,您传递这个**:
$petanidetail = DB::table('tb1_user')->get()->toArray();
return view('listpetani', compact('petanidetail'));
在内部视图中您使用 petanidetail 变量,如下所示:
foreach($petanidetail as $data)
{
echo $data;
}
我希望你已经知道这一点,但也尝试使用模型和控制器, 让路由接受请求,并将其传递给控制器 并使用 Eloquent,这样你的代码就可以这么短:
$petani = PetaniModel::all();
return view('listpetani', compact('petani));
而不是使用
foreach
,将 forelse
与 @empty
语句一起使用,以防 'listpetani'
为空,并向视图提供一些信息,例如“列表为空”。
试试这个:
$petani = DB::table('tbl_user')->pluck('id_user', 'username', 'alamat', 'no_telp', 'id_lokasi');
return view('listpetani', ['petani' => $petani]);
在您的视图上使用
$petani
迭代 foreach()
,例如:
foreach($petani as $data)
{
echo $data->id_user; // $petani is a Std Class Object here
echo $data->username;
}
其他答案是正确的,我只是想添加一件事,如果您的数据库字段有html标签,那么系统将打印html标签,例如段落标签的
< p >
。要解决此问题,您可以使用以下解决方案:
您可能需要将
html_entity_decode
应用于您的数据。
这对于
Laravel 4
来说效果很好
{{html_entity_decode($post->body)}}
对于
Laravel 5
,您可能需要使用这个
{!!html_entity_decode($post->body)!!}