PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 20, 2022

[FIXED] Laravel Eloquent Relationship Join

 January 20, 2022     laravel-8, php     No comments   

Issue

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');
    }
}

Solution

You can try and sort the collection after getting the data from the DB, example:

$products = Product::with('rating')->get()->sortByDesc('rating.ratings');

you can refer to the collection documentation on laravel: https://laravel.com/docs/8.x/collections



Answered By - Marwane Ezzaze
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing