刀片视图不从阵列打印数据

时间:2016-10-12 05:10:30

标签: php arrays laravel-5 laravel-blade

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

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

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

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

  @foreach ($who as $val)

          {{ $val }}

     @endforeach

发生了什么事?

此外,由于数组具有每个值的标题:即“名称”:“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

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

UserEntryController:     

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

USEREDIT:

<?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 = [];

}

3 个答案:

答案 0 :(得分:3)

您只发送对象进行查看。所以尽管那个对象给你1 1 1。

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

更新

将对象转换为数组

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

循环播放该数组

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

从@Mugluck编辑: 快速说明,对于那些得到&#34;未定义变量&#34;在此场景中使用模型类型提示时。我解决了这个问题($ who是路由,在这种情况下是ID):

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

为受损代码道歉。但这应该会给你结果。

答案 1 :(得分:1)

尝试像这样更换你的控制器。这应该将对象传递给刀片:

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

然后你应该能够像你的刀片一样使用

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

更新:同时检查此answer

答案 2 :(得分:0)

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

@foreach ($who as $val)

      {{ print_r($val) }}

 @endforeach