我很想加载这样的画廊
$user = User::where('username', $username)->first();
$favorites = Favorite::with('gallery')->where('user_id', $user->id)->get();
dd($favorites->gallery);
并收到此错误消息:
Undefined property: Illuminate\Database\Eloquent\Collection::$gallery
我最喜欢的课程如下:
class Favorite extends Model
{
protected $table = 'favorites';
public function user(){
return $this->belongsTo(User::class, 'user_id');
}
public function gallery(){
return $this->belongsTo(Gallery::class, 'gallery_id');
}
}
但是,如果我这样做
$user = User::where('username', $username)->first();
$favorites = Favorite::with('gallery')->where('user_id', $user->id)->get();
dd($favorites);
答案 0 :(得分:1)
first()
是一个集合,你无法获得集合的属性。
您需要使用$favorites = Favorite::with('gallery')->where('user_id', $user->id)->first();
从集合中获取第一个对象:
foreach ($favorites as $favorite) {
echo $favorite->gallery;
}
或者迭代收集并获取所有对象:
ListCtrlRevMsg = new wxListCtrl(this, ID_LISTCTRL1, wxPoint(72,8),wxSize(330,100), wxLC_REPORT, wxDefaultValidator, _T("ID_LISTCTRL1"));