获得许多有很多通过

时间:2017-11-30 18:10:52

标签: laravel laravel-5 relationship

我需要通过帖子获得数组用户的最新评论。

用户模型

reduce

has-many-through

// A general purpose, curried validation function
const validate = R.curry((validators, value) =>
  R.reduce((acc, fn) => {
    return acc === false ? acc : fn(value)
  }, true, validators));


const myValidators = [f1, f2, f3]; // Functions return booleans
const myValidator = validate(myValidators);

const isValid = myValidator(myValue); // Returns true or false

许多已经多次通过

public function comments()
 {
   return $this->hasManyThrough('App\Comment', 'App\Post');
 }

我怎样才能做到这一点。

1 个答案:

答案 0 :(得分:0)

您可以在ORM中使用()查找方法以获得预期结果。

例如。 `用户::其中( '字段', 'SOMECONDTION') - >在( '评论');

因此,在您的视图中,您只需获取评论数据。

@foreach($users as $index => $user)
   @foreach($user->comments as $j => $comment)
       {{$comment->some_field}}
   @endforeach
@endforeach