adhik13th
1 month ago
258
8
Laravel

How to show data in relate table laravel

Posted 1 month ago by adhik13th

I have 3 table called category_order , order , order_detail . now i want to showing total_order in another table but its related table ,you can see this table like this :

category_order:

category_order
+----+------------+------------+------------+
| id |  cat_name  | balance    |    some_data
+----+------------+------------+------------+
|  1 |   phone    | 10000000   |    
+----+------------+------------+------------+
|  2 |     PC     | 50000000   |   
+----+------------+------------+------------+
|  3 | Electronic | 500000000  |    
+----+------------+------------+------------+

order:

order
+----+------------+------------+------------+
| id |  cat_id    |some_data   |    some_data
+----+------------+------------+------------+
|  1 |   1        | some_data  |    
+----+------------+------------+------------+
|  2 |   1        | some_data  |   
+----+------------+------------+------------+
|  3 |   2        | some_data  |    
+----+------------+------------+------------+

order_detail

order_detail
+----+------------+------------+------------+
| id |  order_id  | price      |   qty   
+----+------------+------------+------------+
|  1 |   1        |   1000     |    10   // so from this i can create total 10000
+----+------------+------------+------------+
|  2 |   1        |   2000     |    5    // and this 10000
+----+------------+------------+------------+
|  3 |   2        |   5000     |    10   // and this 50000
+----+------------+------------+------------+

this 3 table is related ,and now i want to show data like this :

category_order
+----+------------+------------+------------+
| id |  cat_name  |  balance   |   total_order
+----+------------+------------+------------+
|  1 |   phone    | 10000000   |    20000
+----+------------+------------+------------+
|  2 |     PC     | 50000000   |    50000
+----+------------+------------+------------+
|  3 | Electronic | 50000000   |    . . .  .
+----+------------+------------+------------+

and its my relation

 Models Order
    public function category_order()
    {
        return $this->belongsTo(\App\Models\CategoryOrder::class ,'cat_id');
    }

 Models Order_detail
    public function order()
    {
        return $this->belongsTo(\App\Models\Order::class ,'order_id');
    }

so what i can do for this ???

and my step now is just selecting on category_order like this :

 public function get_belanja()
    {
        $belanja = CategoryOrder::where('status', 'active')->get();
        return response()->json($belanja ,200);
    }

Please sign in or create an account to participate in this conversation.