问题描述
我要打印的用户信息足够简单.
I've got a simple enough array of user information I'm trying to print.
我可以将信息发送到视图中.像这样的命令:
I can send the information to the view fine. And a command like:
{{$ who-> name}}会给我这个名字.
{{ $who->name }} will give me the name.
但是,当我执行一个foreach循环以打印数组中的所有数据时,我得到了一堆数字. 1 1 1和空格.
However when I do a foreach loop to print all the data in the array I get a bunch of numbers. 1 1 1 and blank spaces.
@foreach ($who as $val)
{{ $val }}
@endforeach
这是怎么回事?
此外,由于数组具有每个值的标题:即"Name":"John Doe",有没有办法分别打印标题?
Also, as the array has the titles of each value: ie "Name": "John Doe", is there a way to print the titles separately?
这是控制器:
public function show(UserEdit $object) {
return view('UserEdit', compact('object'));
}
请注意,控制器正在加载模型UserEdit,该模型具有用户的数据,并且ID是从路线生成的.我已经确认有效.
Note the controller is loading the model UserEdit, which has the user's data, and the id is generated from the route. Which I've confirmed works.
更新文件:
UserEdit.blade:
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
给出错误:尝试获取非对象的属性
Gives error: Trying to get property of non-object
UserEntryController:
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 = [];
}
推荐答案
您正在发送的对象仅用于查看.因此,掠过那个物体给你1 1 1.
Your are sending object only to view. So lopping though that object giving you 1 1 1.
这就是为什么它对您有用{{$ obj-> name}}
That's why this is working for you{{ $obj->name }}
将对象转换为数组
public function show(UserEdit $object) {
$object = $object->toArray();
return view('UserEdit', compact('object'));
}
通过该数组循环
@foreach($object as $key=>$value)
{{ $key }} - {{ $value }}
@endforeach
从@Mugluck快速说明,适用于在这种情况下使用模型类型提示时获得未定义的变量"的人.我用这个解决了这个问题($ who是路线,在这种情况下是ID):
Edit from @Mugluck:A quick note, for people who get "Undefined variable" when using the model type hint in this scenario. I solved it with this ($who is the route, which in this case is the ID):
public function show($who) {
$array = UserEdit::find($who)->toArray();
return view('UserEdit', compact('array'));
}
道歉的代码.但这应该给您结果.
Apologies for mangled code. But this should give you the result.
这篇关于刀片视图不打印阵列中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!