【问题标题】:Blade view not printing data from array刀片视图不打印阵列中的数据
【发布时间】:2017-02-20 19:14:14
【问题描述】:

我正在尝试打印一组足够简单的用户信息。

我可以将信息发送到视图。还有一个类似的命令:

{{ $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

给出错误:试图获取非对象的属性

用户入口控制器:

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-&gt;name }} 在 foreach 中。这应该循环所有名称。你得到了 1 1 1 因为你没有指定你想要 $val 显示的内容
  • 我做到了。它抛出了一个错误:尝试获取非对象的属性,因为这是来自模型的数组,我认为我是 Laravel 的新手,所以我还不知道他们是如何做很多事情的。如果我这样做 {{ $who->name}} 我们会得到预期的名称。
  • 那就试试{{ $val['name'] }}
  • 打印空白。有时我喜欢墨菲定律
  • @Rubab 回答对你有用吗?如果不向我们展示该数组中的实际内容。

标签: php arrays laravel-5 laravel-blade


【解决方案1】:

您发送的对象仅供查看。因此,尽管那个物体给你 1 1 1,但它却给了你 1 1 1。

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

更新

将您的对象转换为数组

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

循环遍历该数组

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

从@Mugluck 编辑: 快速说明,对于在这种情况下使用模型类型提示时获得“未定义变量”的人。我用这个解决了它($who 是路由,在这种情况下是 ID):

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

对错误代码表示歉意。但这应该会给你结果。

【讨论】:

  • 那么我将如何更改它以从对象调用数据,或将对象转换为数组。我一直在努力理解 Laravel 处理这些事情的过程。我应该在控制器中更改某些内容吗?或者改变刀片读取对象的方式?
  • 我非常感谢 Rubab 和 @s-i 的帮助。但我仍然得到:尝试获取非对象的属性(视图:...\resources\views\UserEdit.blade.php)我已经编辑了我的主要帖子以说明更改的代码。
  • 您在将该对象转换为视图中的数组后作为对象访问。删除

    用户配置文件数据 {{ $object->First_Name }} {{ $object->Last_Name }}

  • 或者如果你想显示名字和姓氏然后做 $object['First_Name'] 和 $object['Last_Name'] 这是使用键访问数组数据的一般方法跨度>
  • 哦哦。这就是该语法的含义。谢谢你。 :) 我会测试。
【解决方案2】:

尝试像这样更改您的控制器。这应该将一个对象传递给刀片:

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

那么你应该可以像刀片一样使用它

@if ($object)
   @foreach($object as $val)
      {{ $val->name }}
   @endforeach
@endif

更新:也检查一下answer

【讨论】:

  • 这些链接是非常有用的讨论。
  • 不客气。我已经在问题下的评论中提到了如何访问该名称。它与$object['First/Last_Name'] 没有什么不同。很高兴您找到了您一直在寻找的答案。
【解决方案3】:

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

@foreach ($who as $val)

      {{ print_r($val) }}

 @endforeach

【讨论】:

  • 我怀疑他想打印实际的数组。他想使用该数组中的数据。
  • 如果他想使用该数组中的数据,那么他可以使用:{{ $val->name }}。
  • @mahipal-negi 这将使未指定的值加倍。
猜你喜欢
  • 1970-01-01
  • 2021-11-06
  • 2020-05-20
  • 1970-01-01
  • 2015-07-22
  • 2017-09-04
  • 2015-01-11
  • 2012-10-01
  • 2023-03-17
相关资源
最近更新 更多