我正在使用 Laravel 框架 4.2.0,使用这个框架真的很有趣,但我被困在某个地方。
我正在尝试将数据从我的数据库表输出到 Blade View 中。
我已经成功地能够使用查询构建器功能来查询我的数据库表并使用此方法输出数据
我的 Controller
public function index()
{
$records = DB::table('record')->get();
foreach ($records as $record)
{
echo ($record->message);
}
}
然后在我的路线上我查看使用
Route::get('records', 'RecordsController@index');
现在我尝试将这些从查询构建器获得的数据传递到我的 Blade View 中
public function index()
{
$records = DB::table('record')->get();
return View::make('mine')->with('name', '$records');
}
我的 Blade View
@extends('layouts.main')
@section('title', 'testing for data')
@section('content')
<div align="center">
{{name}}
</div>
@endsection
而且我不断收到 whoops 错误页面,我不知道为什么这不起作用,有人可以帮我完成吗
干杯
最佳答案
我正在使用 Laravel 5.2。但我希望这会给你一个想法。
在您的 Controller 中
public function index()
{
$records = DB::table('record')->get();
return view ('Path to your blade template')->with('records',$records);
}
在你的刀锋中
@foreach ($records as $rec)
{{ $rec->message }} //here Code is your JSON object name.
@endforeach
路线保持不变。
Route::get('records', 'RecordsController@index');
当你从数据库中检索数据时。让它更具体。不要获取所有数据(仅在必要时获取)。
关于database - 输出数据库数据并显示在 Blade 模板 View Laravel 中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38421115/