Laravel关于与多个表关联的评论关系

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

Laravel relationship for reviews linked to multiple tables

问题

I am working on a workshop mechanic directory for a client using Laravel 8 framework and he wants a feature to review the workshop and also select multiple mechanics who the client worked with. Each Workshop have multiple reviews and each review can have same or different mechanics who works at the workshop.

  1. Get all the reviews linked to each mechanic like this $mechanic->reviews();

    • 获取与每位技工关联的所有评论,类似于 $mechanic->reviews();
  2. Find top-rated mechanics from all workshops.

    • 查找所有车间中排名最高的技工。

Please let me know how to make this work.
感谢您的协助。

英文:

I am working on a workshop mechanic directory for a client using Laravel 8 framework and he wants a feature to review the workshop and also select multiple mechanics who the client worked with.
Each Workshop have multiple reviews and each review can have same or different mechanics who works at the workshop.

Please help me figure out how to:

  1. Get all the reviews linked to each mechanic like this $mechanic->reviews();
  2. Find top rated mechanics from all workshops.

Please let me know how to make this work.

Thank you so much.

Below is the relationships I made so far and I am not able to figure out the points above.

workshops table
name, email, phone, address

mechanics table
name, email, phont, address, workshop_id

reviews table
name, review, comment, workshop_id, status (active,pending,rejected)

mechanic_review table (for attaching multiple mechanics to each review)
mechanic_id,review_id

Workshop model

public function reviews() {
	return $this->hasMany('App\Models\review');
}

public function mechanics() {
	return $this->hasMany('App\Models\Mechanic');
}


Mechanic model

public function workshop() {
	return $this->belongsTo('App\Models\Workshop');
}


Review model

public function workshop() {
	return $this->belongsTo('App\Models\Workshop');
}

答案1

得分: 0

你可以使用枢轴表 - mechanic_review表来在机械模型中定义关系,以返回机械评论。

{
    return $this->hasMany(Review::class, 'mechanic_review','mechanic_id','review_id');
}```

现在你可以使用$mechanic->mechReviews来访问机械评论。

<details>
<summary>英文:</summary>

You can define a relationship in mechanic model using pivot table - mechanic_review table to return mechanic reviews. 

    public function mechReviews()
    {
        return $this-&gt;hasMany(Review::class, &#39;mechanic_review&#39;,&#39;mechanic_id&#39;,&#39;review_id&#39;);
    }
Now you can access mechanic reviews using $mechanic-&gt;mechReviews .

</details>



# 答案2
**得分**: 0

1. 为了获取与每位技师相关的所有评论,您可以在 Mechanic 模型上定义一个 reviews() 方法,该方法使用 belongsToMany 关系。然后,您可以使用 WITH 访问单个工作坊的所有技师以及相关的评论。

2. 要获取高评分的技师,您需要在评论表中添加一个整数类型的属性,以便您可以获取每位技师的评分数量。然后,您可以通过从评论表中获取评分最高的工作坊中的技师列表来获取单个或多个技师,并将结果限制在1到10或您选择的任何数字。
$mechanics = Mechanic::withCount('reviews')->orderByDesc('ratings')->take(10)->get();

<details>
<summary>英文:</summary>

 1. To get all the reviews linked to each mechanic, you can define a reviews() method on the Mechanic model that uses the belongsToMany relationship. then you can access all the mechanics of a single workshop along with the reviews using WITH.
 2. to get the high rated mechanics you need to add an attribute in reviews table which should be integer type so that you can get the number of rating per mechanic. Then you can get the single or multiple mechanics by getting the list of mechanics from workshops with highest rating from reviews table and you can limit it to any number from 1 to 10 or of your choice.
 $mechanics = Mechanic::withCount(&#39;reviews&#39;)-&gt;orderByDesc(&#39;ratings&#39;)-&gt;take(10)-&gt;get();




</details>



huangapple
  • 本文由 发表于 2023年4月11日 09:24:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981798.html
匿名

发表评论

匿名网友

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

确定