Php Laravel不显示数据
我想显示与Php Laravel不显示数据,php,laravel,Php,Laravel,我想显示与配置文件表链接的用户表中的数据。我在vardump中获得数据,但我不知道如何一对一地显示它。 这是我的职责: public function show($id) { //Get data from user $user = User::find($id); //Get the horse count connect to the user $horse_count = DB::table('horses')->where('user_id', $i
配置文件
表链接的用户
表中的数据。我在vardump中获得数据,但我不知道如何一对一地显示它。
这是我的职责:
public function show($id)
{
//Get data from user
$user = User::find($id);
//Get the horse count connect to the user
$horse_count = DB::table('horses')->where('user_id', $id)->count();
var_dump($user->profile);
die();
}
它返回以下内容:
object(Illuminate\Database\Eloquent\Collection)#188 (1) {
["items":protected]=>
array(1) {
[0]=>
object(App\Profile)#189 (24) {
["connection":protected]=>
NULL
["table":protected]=>
NULL
["primaryKey":protected]=>
string(2) "id"
["keyType":protected]=>
string(3) "int"
["perPage":protected]=>
int(15)
["incrementing"]=>
bool(true)
["timestamps"]=>
bool(true)
["attributes":protected]=>
array(10) {
["id"]=>
int(1)
["user_id"]=>
int(2)
["country"]=>
string(11) "Netherlands"
["age"]=>
string(2) "21"
["recent_online"]=>
string(0) ""
["visitors"]=>
string(4) "2143"
["profile"]=>
string(17) "Hoi ik ben Martin"
["remember_token"]=>
NULL
["created_at"]=>
NULL
["updated_at"]=>
NULL
}
["original":protected]=>
array(10) {
["id"]=>
int(1)
["user_id"]=>
int(2)
["country"]=>
string(11) "Netherlands"
["age"]=>
string(2) "21"
["recent_online"]=>
string(0) ""
["visitors"]=>
string(4) "2143"
["profile"]=>
string(17) "Hoi ik ben Martin"
["remember_token"]=>
NULL
["created_at"]=>
NULL
["updated_at"]=>
NULL
}
["relations":protected]=>
array(0) {
}
["hidden":protected]=>
array(0) {
}
["visible":protected]=>
array(0) {
}
["appends":protected]=>
array(0) {
}
["fillable":protected]=>
array(0) {
}
["guarded":protected]=>
array(1) {
[0]=>
string(1) "*"
}
["dates":protected]=>
array(0) {
}
["dateFormat":protected]=>
NULL
["casts":protected]=>
array(0) {
}
["touches":protected]=>
array(0) {
}
["observables":protected]=>
array(0) {
}
["with":protected]=>
array(0) {
}
["morphClass":protected]=>
NULL
["exists"]=>
bool(true)
["wasRecentlyCreated"]=>
bool(false)
}
}
}
但是当我echo$user->profile->age代码>它没有显示任何内容。
关于信息,我用return$this->belongsTo('App\user')将表user
和profile
链接起来
和返回$this->hasMany('App\Profile')代码>。我得到了数据,但我无法一一显示 一个一个:
foreach (User::find($id)->profile as $profile) {
// Do what you want, e.g.
echo $profile->age;
}
请记住,如果用户有多个配置文件,则关系函数名称应为复数形式,如public function profiles()
此上下文中的配置文件是什么?在这种情况下,您可能需要使用即时加载。查看laravel文档了解更多详细信息。Profile是一个与用户id链接的表table@ClearBoth现在就去看看@清楚这两个都是我正在做的,但它不起作用。它似乎起作用,但我不明白我是如何把它送到刀片的?你能帮我吗?当然可以!您将$user=user::find($id)
-对象传递给视图(例如返回视图('home.index',compact('user'));
。在刀片文件中使用刀片函数:@foreach($user->profile as$profile){{{$profile->age}@endforeach