【问题标题】:laravel & Blade Dynamically load column names from tablelaravel & Blade 从表中动态加载列名
【发布时间】:2017-02-16 04:08:12
【问题描述】:

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

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

我错过了什么?

这是刀片文件:

@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'));
   }
}

【问题讨论】:

  • 尝试打印{{ $name }} 而不是{{ $collumn }}

标签: php database laravel-5 laravel-blade


【解决方案1】:

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

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

仅是您在视图中打印 $columns 的键而不是值(在这种情况下为列名)的原因。仅打印$name

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-05
    • 2016-06-17
    • 1970-01-01
    • 2021-08-20
    • 2016-09-02
    • 1970-01-01
    • 2015-06-11
    • 2016-05-22
    相关资源
    最近更新 更多