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