carts:-
id | product_name | price |
---|
ratings:-
id | product_id | user_id |
---|
I have these two table. And i have defined a relationship in product model for geting rating of particular product. and output of the code is this:-
"data": [
{
"id": 4200,
"name": "Anti gravity Beer Cake",
"modal": "",
"price": 1800,
"discount": 0,
"quantity": 20,
"discription": "Good Quality Product!",
"p_status": "active",
"m_id": 664,
"product_link": "anti-gravity-beer-cake--6040c2b317894",
"product_picture": " ",
"weight": 0,
"weight_type": "Kilogram",
"rating": {
"id": 4,
"u_id": 1,
"product_id": 4200,
"subject": null,
"ratings": 4,
"review": null,
"type": "rating"
}
},
{
"id": 4200,
"name": "Anti gravity Beer Cake",
"modal": "",
"price": 1800,
"discount": 0,
"quantity": 20,
"discription": "Good Quality Product!",
"p_status": "active",
"m_id": 664,
"product_link": "anti-gravity-beer-cake--6040c2b317894",
"product_picture": " ",
"weight": 0,
"weight_type": "Kilogram",
"rating": {
"id": 5,
"u_id": 1,
"product_id": 4200,
"subject": null,
"ratings": 5,
"review": null,
"type": "rating"
}
}
But i want product having high rating first. How i an do it? product model:-
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class product extends Model
{
use HasFactory;
public function rating(){
return $this->hasOne(ratings_and_review::class)->orderBy('ratings','DESC');
}
}
source https://stackoverflow.com/questions/70479479/laravel-eloquent-relationship-join
Comments
Post a Comment