Blade 查看不打印数组中的数据

Blade view not printing data from array

我有一组非常简单的用户信息,我正在尝试打印。

我可以很好地将信息发送到视图。还有一个命令:

{{ $who->name }} 会给我名字。

然而,当我执行 foreach 循环来打印数组中的所有数据时,我得到了一堆数字。 1 1 1 和空格。

  @foreach ($who as $val)

          {{ $val }}

     @endforeach

这是怎么回事?

另外,由于数组有每个值的标题:即"Name":"John Doe",有没有办法单独打印标题?

这是控制器:

 public function show(UserEdit $object) {
       return view('UserEdit', compact('object'));
   }

请注意,控制器正在加载模型 UserEdit,其中包含用户数据,并且 ID 是从路由生成的。我已经确认有效。

编辑:更新文件:

UserEdit.blade:

@extends('layout')


@section('content')
    <h1>User Profile Data {{ $object->First_Name }} {{ $object->Last_Name }}</h1>
        <br><br>

             @foreach ($object as $key=>$value)
                 {{ $key }}  {{ $value }}
             @endforeach


@stop

给出错误:尝试获取 属性 of non-object

用户入口控制器:

namespace App\Http\Controllers;
use App\UserEdit;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;

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

   public function show(UserEdit $object) {
       //$columns = DB::getSchemaBuilder()->getColumnListing('users');
      // $profile = UserEdit::all()->where('ID', '530');
      // $who = UserEdit::find($ID);
       $object = $object->toArray();
       return view('UserEdit', compact('object'));
       //return $object;
   }

}

路线:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index');

Route::get('DB', 'DBController@index');
            //$tables = DB::select('SHOW TABLES');
            //$titles = DB::select("SELECT * FROM topneeds(Name, Abbrev, jobtitle, region, detail)");

            //return view('DB', compact('titles'));
Route::get('Sort', 'SortController@index');
Route::get('User', 'UserEntryController@index');
route::get('User/{object}', 'UserEntryController@show');

用户编辑:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserEdit extends Model  {



    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * Attributes that should be mass-assignable.
     *
     * @var array
     */
    protected $fillable = ['ID', <--67 values-->, 'remember_token'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [];

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [];

}

如果您在 $val 中有数组。所以你不能这样做:-

@foreach ($who as $val)

      {{ print_r($val) }}

 @endforeach

您发送的对象仅供查看。所以通过那个对象给你 1 1 1.

这就是为什么这对你有用 {{ $obj->name }}

更新

将您的对象转换为数组

public function show(UserEdit $object) {
   $object = $object->toArray();
   return view('UserEdit', compact('object'));
}

遍历该数组

@foreach($object as $key=>$value)
   {{ $key }} - {{ $value }}
@endforeach

来自@Mugluck 的编辑: 快速说明,对于在这种情况下使用模型类型提示时获得 "Undefined variable" 的人。我用这个解决了它($who is the route,在这种情况下是ID):

   public function show($who) {
       $array = UserEdit::find($who)->toArray();
       return view('UserEdit', compact('array'));
   }

对于损坏的代码表示歉意。但这应该会给你结果。

尝试像这样更改您的控制器。这应该将 object 传递给 blade:

return view('UserEdit')->with(compact('object'));

那么您应该可以blade像

一样使用它
@if ($object)
   @foreach($object as $val)
      {{ $val->name }}
   @endforeach
@endif

更新:同时检查这个 answer