英文:
How to eagerload 2 different relation in 1 table Laravel
问题
我在Laravel 9中有一个问题,想要在一个表中预加载两个关系。我有一个名为"photos"的表,它与"users"和"photo_categories"有关系。所以我想要同时加载用户和分类。我已经在模型中声明了这些关系。
我已经尝试过以下的代码:
$photos = Photo::with('user','photoCategory')->where('id', $request->id)->first(); //在show方法中使用
$photos = new PhotoCollection(Photo::with('user','photoCategories')->OrderByDesc('id')->paginate(10));//在index方法中使用
我在Photo模型中的关系如下:
public function user(){
return $this->BelongsTo(User::class);
}
public function photoCategory(){
return $this->HasMany(FotoCategory::class);
}
我在用户模型中的关系如下:
public function photo(){
return $this->HasMany(PhotoCategory::class);
}
我在用户photoCategory模型中的关系如下:
public function photo(){
return $this->BelongsTo(Photo::class);
}
请帮助我找出问题所在,提前感谢。
英文:
I have a problem to eagerload 2 relation in 1 table in laravel 9. i have table called photos have relation with users and photo_categories. so what i want to achieve is to load user and categories in same time.also im already declare the relation at the model.
what i already try is like code bellow
$photos = Photo::with('user','photoCategory')->where('id',$request->id)->first(); //at show method
$photos = new PhotoCollection(Photo::with('user','photoCategories')->OrderByDesc('id')->paginate(10));//at index method
my relation at Photo Model
public function user(){
return $this->BelongsTo(User::class);
}
public function photoCategory(){
return $this->HasMany(FotoCategory::class);
}
my relation at user model
public function photo(){
return $this->HasMany(PhotoCategory::class);
}
my relation at user photoCategory model
public function photo(){
return $this->BelongsTo(Photo::class);
}
please help me to figure out the problems
thankyou in advance
答案1
得分: 2
代码部分不需要翻译,以下是已翻译的内容:
It looks like you have issues with your Photo model. The photoCategory method should be a BelongsTo relationship:
在你的照片模型中似乎存在问题。photoCategory 方法应该是一个 BelongsTo 关系:
In user model:
在用户模型中:
Here is final output relationship data:
以下是最终输出的关联数据:
英文:
It looks like you have issues with your Photo model. The photoCategory method should be BelongsTo relationship:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
public function photoCategory()
{
return $this->belongsTo(PhotoCategory::class);
}
<!-- end snippet -->
In user model :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
public function photos()
{
return $this->hasMany(Photo::class);
}
<!-- end snippet -->
Here is final output relationship data :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
$photos = Photo::with('user', 'photoCategory')->where('id', $request->id)->first();
$photos = new PhotoCollection(Photo::with('user', 'photoCategory')->orderByDesc('id')->paginate(10));
<!-- end snippet -->
Hope this will help you.
答案2
得分: 1
在你的代码中似乎有一个拼写错误,你在使用with
方法进行预加载photoCategory
关联时调用了photoCategories
,请尝试按照以下方式更新你的代码:
$photos = Photo::with('user', 'photoCategory')->where('id', $request->id)->first(); // 在show方法中
$photos = new PhotoCollection(Photo::with('user', 'photoCategory')->orderByDesc('id')->paginate(10)); // 在index方法中
此外,请确保你的PhotoCategory
模型具有与Photo
模型的belongsTo
关系,如下所示:
public function photo(){
return $this->belongsTo(Photo::class);
}
英文:
It looks like there is a typo in your code where you are calling photoCategories instead of photoCategory in the with method when eager loading the photoCategory relation.
Try updating your code like this:
$photos = Photo::with('user', 'photoCategory')->where('id', $request->id)->first(); //at show method
$photos = new PhotoCollection(Photo::with('user', 'photoCategory')->orderByDesc('id')->paginate(10)); //at index method
Also, make sure that your PhotoCategory model has a belongsTo relationship to Photo model, like this:
public function photo(){
return $this->belongsTo(Photo::class);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论