php 如何从数据库中获取数据以在laravel中查看页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43090063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get data from database to view page in laravel?
提问by Yosua Simanjuntak
I am using Laravel 5.4 and I want to view my data in database from my view page (listpetani.blade.php
).
我正在使用 Laravel 5.4,我想从我的视图页面 ( listpetani.blade.php
)查看数据库中的数据。
Here is the code of my project:
这是我的项目的代码:
HTML:
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:
In my listpetani.blade.php
I have an empty table and I want to show data from database tbl_user:
PHP:listpetani.blade.php
我有一个空表,我想显示数据库 tbl_user 中的数据 :
Route::get('listpetani', function () {
$petani = DB::table('tbl_user')->pluck('id_user', 'username', 'alamat', 'no_telp', 'id_lokasi');
return view('listpetani', ['petani' => $petani]);
});
And the table in my page: view in browser
以及我页面中的表格:在浏览器中查看
I want to show all the data from database into my view in laravel 5.4. Can anybody help me?
我想在 laravel 5.4 中将数据库中的所有数据显示到我的视图中。有谁能够帮助我?
回答by Yosua Simanjuntak
[SOLVE]
[解决]
Thank you guys, I already solve this problem
谢谢各位,这个问题我已经解决了
This is the solved code
这是解决的代码
web.php (routes)
web.php(路由)
Route::get('listpetani', function () {
$petani = DB::table('tbl_user')->get();
return view('listpetani', ['petani' => $petani]);
});
and in my listpetani.blade.php
在我的 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
回答by Nirbhay Kularia
@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
回答by Hemant Kumar
You can get data from database in view also
您也可以在视图中从数据库中获取数据
@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>
回答by Phantih
In your controller:
在您的控制器中:
$select = DB::select('select * from student');
return view ('index')->with('name',$select);
In Your view:
在您看来:
@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
I hope this can help you.
我希望这可以帮助你。
回答by Kamlesh
Other answers are correct, I just want to add one more thing, if your database field has html tags then system will print html tags like "< p >" for paragraph tag. To remove this issue you can use below solution:
其他答案是正确的,我只想再添加一件事,如果您的数据库字段有 html 标签,那么系统将打印 html 标签,如“< p >”作为段落标签。要解决此问题,您可以使用以下解决方案:
You may need to apply html_entity_decode to your data.
您可能需要将 html_entity_decode 应用于您的数据。
This works fine for Laravel 4
这适用于 Laravel 4
{{html_entity_decode($post->body)}}
{{html_entity_decode($post->body)}}
For Laravel 5 you may need to use this instead
对于 Laravel 5,您可能需要使用它来代替
{!!html_entity_decode($post->body)!!}
{!!html_entity_decode($post->body)!!}
回答by ram pratap singh
**In side controller you pass this **:
**在侧控制器中你通过这个**:
$petanidetail = DB::table('tb1_user')->get()->toArray();
return view('listpetani', compact('petanidetail'));
and Inside view you use petanidetail variable as follow:
和内部视图您使用 petanidetail 变量如下:
foreach($petanidetail as $data)
{
echo $data;
}
回答by Mayank Pandeyz
Try this:
尝试这个:
$petani = DB::table('tbl_user')->pluck('id_user', 'username', 'alamat', 'no_telp', 'id_lokasi');
return view('listpetani', ['petani' => $petani]);
on your view iterate $petani
using foreach()
like:
在您的视图中$petani
使用foreach()
如下迭代:
foreach($petani as $data)
{
echo $data->id_user; // $petani is a Std Class Object here
echo $data->username;
}