大家好,我是Laravel 4 PHP Framework的新手,我知道一点点。只是想问一下如何热切地加载3个表(或更多)并给视图一个干净的方式访问变量(我有一个表用户,配置文件和状态)
**Users Table**
id (int) ai
code (varchar)
email_id (int) ref to email table
username (varchar)
password (varchar)
softdeletes, timestamps, remember token laravel defaults
**Profile Table**
id (int) ai
user_id (int) index ref to users table
lastname (varchar)
firstname (varchar)
middlename (varchar)
birthdate (date)
and more...
laravel defaults...
**Statuses Table**
id (int) ai
user_id (int) index ref to users table
message (text)
laravel defaults...
laravel提供的用户模型我只需添加公共功能配置文件和状态
<?php
class User extends \Eloquent {
......
public function profile()
{
return $this->hasOne('Profile');
}
public function statuses()
{
return $this->hasMany('Status');
}
}
个人资料和状态模型
class Profile extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
}
class Status extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
}
在我的HomeController索引方法中,我已经声明了一个像这样的变量$ user。
$user = User::with('profile', 'statuses')->whereId( Auth::id() )->first();
$statuses = $user->statuses;
然后我将状态压缩到我的视图中,然后运行
@foreach($statuses as $status)
{{ $status->user->profile->firstname; }}
{{ $status->message; }}
@endforeach
但我收到了错误。
谢谢:)
答案 0 :(得分:0)
如果我在Blade视图中理解你应该使用:
{{ $user->profile->firstname }} {{ $user->profile->lastname }}
@foreach ($user->statuses as $status)
{{ $status->message }} {{ $status->id}}
@endforeach
获取所选用户的所有状态消息和ID。
修改强>
在添加更多代码时,您应该为视图分配$user
和$statuses
变量。
现在你可以做到:
@foreach ($statuses as $status)
{{ $user->profile->firstname }} {{ $user->profile->lastname }}
{{ $status->message }}
@endforeach
因为这些消息的每个用户都是相同的(您从具有所选ID的数据库用户那里获得)
但是您只能将$user
分配给您的视图,以下代码应该有效:
@foreach ($user->statuses as $status)
{{ $user->profile->firstname }} {{ $user->profile->lastname }}
{{ $status->message }}
@endforeach
答案 1 :(得分:0)
如何获取具有状态的用户的名字和姓氏 与那些相关联的
然后你可以试试这个(注意User::has('statuses')
,这将只有那些拥有statuses
的用户才能获得:
$user = User::has('statuses')
->with('profile', 'statuses')
->whereId(Auth::id())
->first();
如果没有has('statuses')
,即使他们没有相关的状态,您也会获得所有用户。然后在view
:
{{ $user->profile->firstname }}
{{ $user->profile->lastname }}
答案 2 :(得分:0)
你需要知道,雄辩的关系不是双向的。
话虽如此,在statuses
上加载users
不会在每个user
上加载status
:
$user = User::with('statuses')->first();
// query users table once and statuses table once
// now you can use statuses:
$user->statuses; // collection
$user->statuses->first(); // single status
但这是完全不同的故事
$user->statuses->first()->user;
// query users table for the user of this status
// despite you already loaded that user and stored in $user variable
// in your case you did that in your loop:
@foreach($statuses as $status)
{{ $status->user->profile->firstname; }}
// here you query users table AND profiles table again
因此,如果你在foreach循环中执行此操作,那么最终会得到尽可能多的查询,因为集合中存在状态。
现在,最简单,最具表现力的方式就是:
// controller
$user = Auth::user()->load('statuses', 'profile');
// the same as User::with('statuses', 'profile')->where('id', Auth::id())->first();
return View::make('some.view', compact('user'));
然后在你看来:
@foreach ($user->statuses as $status)
// you can access the user, not affected by the loop
$user->username
// you can access the profile, not affected by the loop
$user->profile->firstname
// and of course each status in the loop
$status->message
@endforeach