输出数据库数据并显示在刀片模板视图 Laravel 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38421115/
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
output database data and display in blade template view laravel
提问by tee boi
I am working with Laravel framework 4.2.0, its really interesting working with this framework, but i am stuck somewhere.
我正在使用 Laravel 框架 4.2.0,使用这个框架真的很有趣,但我被困在某个地方。
I am trying to output data into a blade view from my database table.
我正在尝试将数据从我的数据库表输出到刀片视图中。
i have successfully been able to use the query builder feature to query my database table and output data using this method
我已经成功地使用查询构建器功能来查询我的数据库表并使用此方法输出数据
My controller
我的控制器
public function index()
{
$records = DB::table('record')->get();
foreach ($records as $record)
{
echo ($record->message);
}
}
and then on my route i view using
然后在我的路线上我查看使用
Route::get('records', 'RecordsController@index');
Now i try to pass this data gotten from the query builder to my blade view like this
现在我尝试将这些从查询构建器获得的数据传递到我的刀片视图中
public function index()
{
$records = DB::table('record')->get();
return View::make('mine')->with('name', '$records');
}
my blade view
我的刀片视图
@extends('layouts.main')
@section('title', 'testing for data')
@section('content')
<div align="center">
{{name}}
</div>
@endsection
And i keep getting the whoops error page, i have no idea why this is not working, can someone please put me through
而且我不断收到 whoops 错误页面,我不知道为什么这不起作用,有人可以帮我完成吗
Cheers
干杯
采纳答案by Sachith Muhandiram
I am using Laravel 5.2. But I hope this will give you an idea.
In your controller
我正在使用 Laravel 5.2。但我希望这会给你一个想法。
在您的控制器中
public function index()
{
$records = DB::table('record')->get();
return view ('Path to your blade template')->with('records',$records);
}
In your Blade
在你的刀锋中
@foreach ($records as $rec)
{{ $rec->message }} //here Code is your JSON object name.
@endforeach
Route stays same.
路线保持不变。
Route::get('records', 'RecordsController@index');
When you retrieve data from database.Make it more specific. Do not take all data (take only if necessary).
当你从数据库中检索数据时。让它更具体。不要获取所有数据(仅在必要时获取)。