database - 输出数据库数据并在 blade 模板 View laravel 中显示

标签 database laravel

我正在使用 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

我一直收到糟糕的错误页面,我不知道为什么这不起作用,有人可以帮我接通吗

干杯

最佳答案

我正在使用 Laravel 5.2。但我希望这会给你一个想法。
在你的 Controller 中

public function index()
{
$records = DB::table('record')->get();
return view ('Path to your blade template')->with('records',$records); 
}

在你的 Blade 中

@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/

相关文章:

mysql - 数据库规范化: Using separate tables to store a single field

php - 存储过程 SQL 转换为 MySQL

node.js - Mongoose 双向模型/多对多关系

php - 如何使用 HTTP Client 将图像或文件发送到外部 API?

php - 修补匠在Laravel 5.8中给出了异常(exception)

.net - Visual Studio 上的 ezdb 文件?

sql - 如何在不列出列的情况下对每一列执行相同的聚合?

php - 在 Laravel-5 中取消定义方法?

php - 无法在 laravel 项目中安装 Composer

php - laravel 5 在 Controller 中获取默认语言