Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GodziLaravel's avatar

How to add manually elements to the eloquent result ?

Hello , this is my eloquent query .

$this->plannings = DB::table("plannings as p")
		->select(...)
		->orderByRaw(" u.name asc, date asc , quarter asc")
		->get()

the result is :

Illuminate\Support\Collection {#1603 ▼
  #items: array:8 [▼
    0 => {#1473 ▼
      +"dep_id": 6
      +"user_id": 6
      +"user_name": "Abc name"
      +"date": "2022-11-01"
      +"quarter": "Q1"
      +"comment": null
      +"to_invoice": false
      +"adhoc": false
      +"flight": false
      +"telework": "On customer site"
      +"teamleader_company_name": "Public holiday"
      +"activity_type_name": null
      +"teamleader_deal_name": null
      +"planning_absence_id": 6478
      +"label_hr": "Public hol."
      +"label": "Public holiday"
    }
    1 => {#1477 ▼
      +"dep_id": 6
      +"user_id": 6
      +"user_name": "Abc name"
      +"date": "2022-11-01"
      +"quarter": "Q2"
      +"comment": null
      +"to_invoice": false
      +"adhoc": false
      +"flight": false
      +"telework": "On customer site"
      +"teamleader_company_name": "Public holiday"
      +"activity_type_name": null
      +"teamleader_deal_name": null
      +"planning_absence_id": 6478
      +"label_hr": "Public hol."
      +"label": "Public holiday"
    }
    2 => {#1459 ▼
      +"dep_id": 6
      +"user_id": 6
      +"user_name": "Abc name"
      +"date": "2022-11-01"
      +"quarter": "Q3"
      +"comment": null
      +"to_invoice": false
      +"adhoc": false
      +"flight": false
      +"telework": "On customer site"
      +"teamleader_company_name": "Public holiday"
      +"activity_type_name": null
      +"teamleader_deal_name": null
      +"planning_absence_id": 6478
      +"label_hr": "Public hol."
      +"label": "Public holiday"
    }
    3 => {#1457 ▼
      +"dep_id": 6
      +"user_id": 6
      +"user_name": "Abc name"
      +"date": "2022-11-01"
      +"quarter": "Q4"
      +"comment": null
      +"to_invoice": false
      +"adhoc": false
      +"flight": false
      +"telework": "On customer site"
      +"teamleader_company_name": "Public holiday"
      +"activity_type_name": null
      +"teamleader_deal_name": null
      +"planning_absence_id": 6478
      +"label_hr": "Public hol."
      +"label": "Public holiday"
    }
    4 => {#1476 ▼
      +"dep_id": 6
      +"user_id": 6
      +"user_name": "Abc name"
      +"date": "2022-11-11"
      +"quarter": "Q1"
      +"comment": null
      +"to_invoice": false
      +"adhoc": false
      +"flight": false
      +"telework": "On customer site"
      +"teamleader_company_name": "Public holiday"
      +"activity_type_name": null
      +"teamleader_deal_name": null
      +"planning_absence_id": 6479
      +"label_hr": "Public hol."
      +"label": "Public holiday"
    }
    5 => {#1443 ▼
      +"dep_id": 6
      +"user_id": 6
      +"user_name": "Abc name"
      +"date": "2022-11-11"
      +"quarter": "Q2"
      +"comment": null
      +"to_invoice": false
      +"adhoc": false
      +"flight": false
      +"telework": "On customer site"
      +"teamleader_company_name": "Public holiday"
      +"activity_type_name": null
      +"teamleader_deal_name": null
      +"planning_absence_id": 6479
      +"label_hr": "Public hol."
      +"label": "Public holiday"
    }
    6 => {#1627 ▶}
    7 => {#1625 ▶}
  ]
  #escapeWhenCastingToString: false
}

As you can see for user_id:6 and the date:'2022-11-01' I have all quarters :Q1,Q2,Q3 and Q4 so it's okay But for the user user_id:6 and the date:'2022-11-02' for example there is no quarters rows , so I need to add 4 elements like :

[  "user_id" => null, "user_name" => "Abc name","date" => "2022-11-02","quarter" => "Q1"],
[  "user_id" => null, "user_name" => "Abc name","date" => "2022-11-02","quarter" => "Q2"],
[  "user_id" => null, "user_name" => "Abc name","date" => "2022-11-02","quarter" => "Q3"],
[  "user_id" => null, "user_name" => "Abc name","date" => "2022-11-02","quarter" => "Q4"]        

Any Idea ? from where can I start?

0 likes
3 replies
Tray2's avatar

Well, the database can only give you what is in the database, so if one user only has two quarters, the query will only return two quarters.

You need to handle that in your model, I suggest writing a presentation method, that analyses the records, and add the empty ones where needed.

Sinnbeck's avatar

Be aware that your code isn't eloquent. It's the query builder. So you cannot use accessors or appends

1 like

Please or to participate in this conversation.