如何在 Laravel 中预加载一个表中的两个不同关系

huangapple go评论120阅读模式
英文:

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-&gt;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-&gt;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(&#39;user&#39;, &#39;photoCategory&#39;)-&gt;where(&#39;id&#39;, $request-&gt;id)-&gt;first();

$photos = new PhotoCollection(Photo::with(&#39;user&#39;, &#39;photoCategory&#39;)-&gt;orderByDesc(&#39;id&#39;)-&gt;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(&#39;user&#39;, &#39;photoCategory&#39;)-&gt;where(&#39;id&#39;, $request-&gt;id)-&gt;first(); //at show method

$photos = new PhotoCollection(Photo::with(&#39;user&#39;, &#39;photoCategory&#39;)-&gt;orderByDesc(&#39;id&#39;)-&gt;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-&gt;belongsTo(Photo::class);
}

huangapple
  • 本文由 发表于 2023年3月9日 19:12:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定