I am looking to get an array of the amounts of the income of all the existing years.
Here's the Income table:
public function up()
{
Schema::create('income', function (Blueprint $table) {
$table->id();
$table->string('description');
$table->bigInteger('amount');
$table->date('date');
$table->string('receipt')->nullable();
$table->foreignId('user_id')->nullable()->constrained('users')->onDelete('cascade');
$table->timestamps();
});
}
Then the Eloquent queries to get the Years existing in the date column:
$years = Income::selectRaw('YEAR(income.date) AS year')->orderByRaw('date ASC')->groupBy('year')->pluck('year');
$array = (array) $years;
$array gives the result = [2010,2012,2018,2019,2020,2021,2022]
But this is where there's an issue:
foreach ($years as $year){
$yearlyIncome = Income::select('amount',DB::raw('YEAR(income.date) as year'))->orderByRaw('year ASC')->groupBy('amount','year')->pluck('amount');
}
I get $yearlyIncome = [3000,450,2000,6800,500,2800,4000,1500,5000]
So the incomes belonging to the same years have not been added together, how can i correct it ?