Laravel雄辩有很多->;有许多
是否可以在一个集合/json中获取所有详细信息(用户、帖子、帖子) 用户->有许多(帖子) post->hasmany(postmage) 用户: id |名称 职位: id |用户| id |文本 后期图像: id | post | id | imgpath 用户模型:Laravel雄辩有很多->;有许多,laravel,eloquent,Laravel,Eloquent,是否可以在一个集合/json中获取所有详细信息(用户、帖子、帖子) 用户->有许多(帖子) post->hasmany(postmage) 用户: id |名称 职位: id |用户| id |文本 后期图像: id | post | id | imgpath 用户模型: public function posts() { return $this->hasMany('App\posts')->orderBy('id', 'ASC'); } 职位模式: p
public function posts() {
return $this->hasMany('App\posts')->orderBy('id', 'ASC');
}
职位模式:
public function images(){
return $this->hasMany('App\postsimages');
}
public function images()
{
return $this->hasMany('App\postsimages');
}
从用户处获取所有帖子工作正常:
$myposts = users::find(Auth::user()->id)->posts;
我能够在一个循环内从帖子中获取所有图像
foreach($myposts as $mypost) {
$postimages = $mypost->images;
}
我想要的是得到所有的帖子,没有循环的图片
$myposts = users::find(Auth::user()->id)->posts->images
谢谢是的,使用关系
职位模式:
public function images(){
return $this->hasMany('App\postsimages');
}
public function images()
{
return $this->hasMany('App\postsimages');
}
用户模型
public function posts()
{
return $this->hasMany('App\posts')->orderBy('id', 'ASC');
}
public function images()
{
return $this->hasManyThrough('App\posts', 'App\postsimages');
}
然后
try:$myposts=users::find(Auth::user()->id)->posts()->image;未定义的属性:Illumb\Database\Eloquent\Relations\HasMany:$imagesTry
$myposts=users::find(Auth::user()->id)->posts->with('images')->get()
@Rifki Auth::user()已经是用户对象了。没有理由再次找到模型Auth::user()->posts…
@markus抱歉,请尝试load()
,这样会是Auth::user()->posts->load('images')