laravel & Blade 从 table 动态加载列名

laravel & Blade Dynamically load column names from table

我正在尝试填充我的用户配置文件表单,由于数据库 table 有很多列,我正在尝试查看是否可以自动加载它们。

我有点让它工作了。但目前它打印出一个数字列表(我假设的 table 列 ID),而不是列的名称。

我错过了什么?

这是 blade 文件:

@extends('layout')


@section('content')
    <h1>User Profile Data</h1>

        @foreach ($columns as $column => $name)
            {{ $column }}

        @endforeach

            @foreach ($profile as $person)
                @foreach ($person as $name)
                    {{ $name }}<br>
                @endforeach
            @endforeach

@stop

这是控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;

class UserEntryController extends Controller
{
   public function index(){
       $columns = DB::getSchemaBuilder()->getColumnListing('users');
       $profile = DB::table('users')->where('ID', '682')->get();
       return view('UserEntry', compact('profile', 'columns'));
   }
}

I sort of have it working. But at the moment it prints out a list of numbers (the table column ID I assume), rather than the names of the columns.

不,您看的不是 "table column ID",而是数字是 $columns 数组的键。清楚地看到您只获取了列名而不是控制器中的列(id)的值。

这只是您在视图中打印键而不是 $columns 的值(在本例中为列名)的原因。仅打印 $name

@foreach ($columns as $column => $name)
   {{ $name }}
@endforeach