【发布时间】:2020-01-18 07:22:50
【问题描述】:
在后期模型中
function user()
{
return $this->belongsTo( \App\User::class);
}
在用户模型中
function posts()
{
return $this->hasMany( \App\Post::class);
}
function somedata()
{
return date('i') * 1000 + date('s');
}
在控制器中
$posts = Post::query()
->where('id', 10)
->with('user')
->get();
但它没有在用户模型中获得“某些数据”。
如何将这些数据与帖子一起拖动?
【问题讨论】:
-
你试过
$posts->first()->somedata();。$posts应该是Collection的对象。 -
您的 $posts 是集合,因此您需要使用循环 $posts 作为 $post 并访问 $post->user->somedata 方法