通过 N 个模型的 hasMany 或 hasManyThrough 链
Chain of hasMany or hasManyThrough through N models
如何获取属于特定用户的所有提交?
试试这个:
$user=User::find(1);
dd($user->submissions);
抛出错误:
Call to undefined method Illuminate\Database\Query\Builder::hasMany()
我必须循环遍历模型吗?
以下是模型:
class User extends Eloquent implements ConfideUserInterface, BillableInterface
{
public function categories()
{
return $this->hasMany('Category');
}
public function forms()
{
return $this->hasManyThrough('App\Models\Form', 'Category');
}
public function submissions()
{
return $this->hasMany('Category')->hasMany('Form')->hasMany('Submission');
}
}
class Category extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
public function forms()
{
return $this->hasMany('App\Models\Form');
}
public function submissions()
{
return $this->hasManyThrough('Submission', 'App\Models\Form', 'id', 'form_id');
}
}
namespace App\Models;
class Form extends \Eloquent {
public function user()
{
return $this->category->user();
}
public function category()
{
return $this->belongsTo('Category');
}
public function submissions()
{
return $this->hasMany('Submission');
}
}
class Submission extends \Eloquent {
public function user()
{
return $this->form->category->user();
}
public function category()
{
return $this->form->category();
}
public function form()
{
return $this->belongsTo('App\Models\Form');
}
}
对于链接关系,这并不是真正有效的...
你能做的,是这样的:
$submissions = Submission::whereHas('form.category.user', function($q){
$q->where('id', 1);
})->get();
请注意 具有嵌套关系的 whereHas
仅在最新的 Laravel 4 版本中添加。确保 composer update
.
如果我没听错你的问题,你需要正确定义关系。
根据laravel 4.2,您可以使用以下代码来定义关系:
class User extends \Eloquent implements ConfideUserInterface, BillableInterface{
//...
public function submissions(){
return $this->hasMany('Submission');
}
}
//...
class Submission extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
//...
}
要进一步阅读,请查看:http://laravel.com/docs/4.2/eloquent#relationships
如何获取属于特定用户的所有提交? 试试这个:
$user=User::find(1);
dd($user->submissions);
抛出错误:
Call to undefined method Illuminate\Database\Query\Builder::hasMany()
我必须循环遍历模型吗?
以下是模型:
class User extends Eloquent implements ConfideUserInterface, BillableInterface
{
public function categories()
{
return $this->hasMany('Category');
}
public function forms()
{
return $this->hasManyThrough('App\Models\Form', 'Category');
}
public function submissions()
{
return $this->hasMany('Category')->hasMany('Form')->hasMany('Submission');
}
}
class Category extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
public function forms()
{
return $this->hasMany('App\Models\Form');
}
public function submissions()
{
return $this->hasManyThrough('Submission', 'App\Models\Form', 'id', 'form_id');
}
}
namespace App\Models;
class Form extends \Eloquent {
public function user()
{
return $this->category->user();
}
public function category()
{
return $this->belongsTo('Category');
}
public function submissions()
{
return $this->hasMany('Submission');
}
}
class Submission extends \Eloquent {
public function user()
{
return $this->form->category->user();
}
public function category()
{
return $this->form->category();
}
public function form()
{
return $this->belongsTo('App\Models\Form');
}
}
对于链接关系,这并不是真正有效的...
你能做的,是这样的:
$submissions = Submission::whereHas('form.category.user', function($q){
$q->where('id', 1);
})->get();
请注意 具有嵌套关系的 whereHas
仅在最新的 Laravel 4 版本中添加。确保 composer update
.
如果我没听错你的问题,你需要正确定义关系。
根据laravel 4.2,您可以使用以下代码来定义关系:
class User extends \Eloquent implements ConfideUserInterface, BillableInterface{
//...
public function submissions(){
return $this->hasMany('Submission');
}
}
//...
class Submission extends \Eloquent {
public function user()
{
return $this->belongsTo('User');
}
//...
}
要进一步阅读,请查看:http://laravel.com/docs/4.2/eloquent#relationships