如何传递多个 table 数据以在 laravel 5.2 中查看

how to pass multiple table data to view in laravel 5.2

我在数据库中有 3 个 table,

--用户

--简介

--教育

用户模型

 public function profiledetails()
{
        return $this->hasOne('App\Profiledetails');
}

public function educationHasOne()
{
        return $this->hasOne('App\Education');
}

资料详情模型

public function user()
{
        return $this->belongsTo('App\User');
}

教育模式

public function user()
{
        return $this->belongsTo('App\User');
}

我可以存储数据。

我想在一个网页中查看所有 table 数据 我使用下面的代码来实现这一点。

  public function index()
 {
 $user = Auth::user()->id;
 $profile = array(
     User::find($user)->profiledetails,
     User::find($user)->educationHasOne 
 );
 return view('profile.index', ['profile' => $profile]);
}

当使用 dd($variablename) 时,我能够看到我需要的所有必填字段,

现在,我想知道如何将所有这些数据传递到单个页面中查看。

您可以将数组传递给视图,如下所示:

return view('profile', ['profile' => $profile]);

在视图中,您可以访问$profile

如果每个都需要这个数据,建议使用view composers。您可以在这里找到不错的文档和示例:https://laravel.com/docs/5.1/views#passing-data-to-views

你可以使用@Schellingerht 回答甚至使用 "compact"

$profile= 'someVar';
return view( 'profile',compact('profile') );

这里不需要创建关联数组。只要确保变量名与 compact 中的字符串相同即可。 在你看来你只是用

来称呼它
{{$profile}}

感谢@Schellingerht,将数组传递给视图效果很好。

当用户登录时代码运行完美。

下面是我的代码

pass an array to the view

public function index()
{

 $user = Auth::user()->id;
 $profile   = User::find($user)->profiledetails;

 $education = User::find($user)->educationHasOne;
 return view('profile.index',['profile' => $profile, 'education' => $education]); 

}