Add 2 array values in Laravel - laravel

I a, new learner and working on home project. I stuck to to get additions of 2 array values . The array values are as follows,
Thanks for your help.. Thanks u
Array 1
array:1 [▼
0 => array:3 [▼
"totalbmilk" => "168.00"
"totala2milk" => "0.00"
"totaljmilk" => "390.00"
]
]
Array 2
array:1 [▼
0 => array:3 [▼
"totalbmilk" => "8.00"
"totala2milk" => "5.50"
"totaljmilk" => "2.50"
]
]
Expecting
totalbmilk = 168 + 8 = 176
totala2milk = 0 + 5.5 = 5.50
totaljmilk = 390 + 2.50 = 392.50
Controller file
$milksalefordairy = Dairymilksale::selectraw('
SUM(buffalomilk) as "totalbmilk",
SUM(a2milk) as "totala2milk",
SUM(jerseymilk) as "totaljmilk"
')
->whereBetween('saledate', [$startdateofmonth, $enddateofmonth])
->get()
->toArray();
$milksaleforcustomer = Customermilksale::selectraw('
SUM(buffalomilk) as "totalbmilk",
SUM(a2milk) as "totala2milk",
SUM(jerseymilk) as "totaljmilk"
')
->whereBetween('saledate', [$startdateofmonth, $enddateofmonth])
->get()
->toArray();

You could use collection methods.
$milksalefordairy = Dairymilksale::selectraw('
SUM(buffalomilk) as "totalbmilk",
SUM(a2milk) as "totala2milk",
SUM(jerseymilk) as "totaljmilk"
')
->whereBetween('saledate', [$startdateofmonth, $enddateofmonth])
->get()
->toArray();
$milksaleforcustomer = Customermilksale::selectraw('
SUM(buffalomilk) as "totalbmilk",
SUM(a2milk) as "totala2milk",
SUM(jerseymilk) as "totaljmilk"
')
->whereBetween('saledate', [$startdateofmonth, $enddateofmonth])
->get()
->toArray();
$collection = collect([...$milksalefordairy, ...$milksaleforcustomer]);
echo $collection->sum('totalbmilk');
echo $collection->sum('totala2milk');
echo $collection->sum('totaljmilk');

Related

Foreach only showing First item in array

I am trying to get data for each id using foreach. But when code run it get only data for 1 ID.. Following is the code
$buffalidforavgmilk = Buffalodata::groupBy('buffaloID')->get('buffaloID')- >pluck('buffaloID')->toArray();
foreach ($buffalidforavgmilk as $id) {
$milkperid = Buffalomilkrecord::where('buffaloID', $id)->pluck('buffaloID')->toArray();
dd([$buffalidforavgmilk,$milkperid]);
}
Output
array:2 [▼
0 => array:4 [▼
0 => "Buffalo-01"
1 => "Buffalo-02"
2 => "Buffalo-03"
3 => "Buffalo-04"
]
1 => array:5 [▼
0 => "Buffalo-01"
1 => "Buffalo-01"
2 => "Buffalo-01"
3 => "Buffalo-01"
4 => "Buffalo-01"
]
]
Here Loop giving only 1 ID where as required array for all 4 ID
( for Test, i try to get only buffaloID)
Thanks in Advance
dd interrupts execution. If you wanted to dump every result and then stop execution, you should have used dump instead
foreach ($buffalidforavgmilk as $id) {
$milkperid = Buffalomilkrecord::where('buffaloID', $id)->pluck('buffaloID')->toArray();
dump([$buffalidforavgmilk,$milkperid]);
}
dd('Done');
This is not ideal though. You are making a query in each iteration of the loop.
One way you could remove the foreach is to change your query to use whereIn.
$buffalidforavgmilk = Buffalodata::groupBy('buffaloID')->pluck('buffaloID')->toArray();
$milkperids = Buffalomilkrecord::whereIn('buffaloID', $buffalidforavgmilk)->pluck('buffaloID')->toArray();

array in where condition in laravel

hello i am facing problem in where condition laravel.
i have two array
$a = [▼
0 => 2
1 => 9
2 => 39
3 => 174
4 => 190
];
$b = [▼
0 => 2
1 => 9
2 => 39
3 => 174
4 => 190
5 => 0
];
**array values is ids. i tried array_values($a) && array_values($b)
i want ids like this but not get same response.**
[ 2 ,9 ,39 ,174 ,190] && [ 2 ,9 ,39 ,174 ,190 , 0 ]
trying to get data from query like
$classrooms = Classroom::where('teacher_id', $teacherId)
->where('school_curriculum_id', [$b])
->where('hybrid_curriculum', [$b])->get();
but got null . something wrong in where condition.
please help me to solve this.
protected $fillable = [
'id', 'school_id', 'grade_id', 'teacher_id', 'school_curriculum_id', 'name', 'deleted', 'deleted_date', 'archived', 'is_hybrid', 'hybrid_curriculum', 'hybrid_grade', 'enable_auto_submit_assessment', 'timeline_type', 'timeline_modified', 'group_test_by', 'created', 'modified', 'enable_auto_lockout_unit',
];
Can you try this:
$classrooms = Classroom::where('teacher_id', $teacherId)
->whereIn('school_curriculum_id', $b)
->whereIn('hybrid_curriculum', $b)->get();

Laravel 5.4 whereNotIn apparently not working

I have logic in a controller that builds an array called $exclude.
Using dd for $exclude I get :
array:4 [▼
0 => 2
1 => 3
2 => 4
3 => 5
]
which is correct.
I want to exclude those from a result so I have:
$potype = DB::table('potypes')
->whereNotIn('id',[$exclude])
->get();
but when I run the query those items are included with the exception of the first in the array. So I enabled the query log with
DB::enableQueryLog();
and ran
dd(DB::getQueryLog());
with the result of
array:1 [▼
0 => array:3 [▼
"query" => "select * from `potypes` where `id` not in (?)"
"bindings" => array:4 [▼
0 => 2
1 => 3
2 => 4
3 => 5
]
"time" => 0.67
]
]
The table has 8 records but running the query is returning 7, only ommiting the first of the list:
Collection {#621 ▼
#items: array:7 [▼
If I use implode
$ex = implode(',',$exclude)
and change the query to
->whereNotIn('id',[$ex])
I get the same result - 7 items with just the first in the list being ignored.
Is this an Eloquent bug or me?
delete [ ] and check it again:
$potype = DB::table('potypes')
->whereNotIn('id',$exclude)
->get();
OK it was realtively simple with Mohammed's comment pointing me in the right direction.
As $exclude was an array I changed the get to:
$potype = DB::table('potypes')
->whereNotIn('id',$exclude)
->get();
and it worked OK!

how to make multi dimension array from sql return rows in codeigniter

how to make multi dimension array from sql return rows in codeigniter.
return values from model holding all values in $res.
$res = $this->user_model->get_room_book_join(['rooms.hotel_id' => 1]);
if ($res) {
echo '<pre>';
print_r($res);
}
I getting this type of array.this type can not helping me.
Array( [0] => Array
(
[room_id] => 1
[room_no] => 101
[room_desc] => Double Bed Deluxe Non Air Conditioned
[status] => available
[category_id] => 1
[hotel_id] => 1
[tariff_type] => normal
[room_rate] => 1000
[persons] => 0
[date_start] => 0000-00-00
[date_end] => 0000-00-00
[overview_id] => 1
[rom_id] => 1
[hot_id] => 1
[cus_id] => 2
[bok_id] => 2
[dates] => 2017-04-12
)
[1] => Array
(
[room_id] => 2
[room_no] => 101
[room_desc] => Double Bed Deluxe Non Air Conditioned
[status] => available
[category_id] => 1
[hotel_id] => 1
[tariff_type] => normal
[room_rate] => 1000
[persons] => 0
[date_start] => 0000-00-00
[date_end] => 0000-00-00
[overview_id] => 1
[rom_id] => 1
[hot_id] => 1
[cus_id] => 2
[bok_id] => 2
[dates] => 2017-04-13
)
)
Actually I want this type of array.how can i make it.please help advance thanks.
array(
[0]=>array(
[room_id]=>1
[dates]=>array(
[0]=>2014-04-12
[1]=>2014-04-13
)
)
[1]=>array(
[room_id]=>2
[dates]=>array(
[0]=>2014-04-12
[1]=>2014-04-13
)
)
)
Hi, you can try this code, this may help you
$query = $this->db->get();
$result = $query->result_array();
$main_array=array();
$actual_array=array();
foreach($result as $res){
$main_array['room_id']=$res['room_id'];
$main_array['dates']=array($res['date_start'],$res['date_end']);
$actual_array[]=$main_array;
}
//echo ""; print_r($actual_array);
return $actual_array;
try this
// after if ($res) {
for ($i=0; $i < count($res); $i++) {
$res[$i] = array(
'room_id' => $res[$i]['room_id'],
'dates' => array($res[$i]['date_start'],$res[$i]['date_end'])
);
}

Laravel - Group By same day ( Mon , Tue , etcc)

I have the following table:
TABLE:schedule
id day name
-----------------------------
1 Mon test
2 Mon hello
3 Tue another
4 Tue here
5 Wed go
I have a Laravel model for this , so I am trying to group day
here is the result I would like to have as an object:
['Mon']-> array[0]='name'=>test,
array[1]='name'=>hello,
['Tue']-> array[0]='name'=>another,
array[1]='name'=>here,
here is my code in the HomeController:
$schedule = DB::table('schedule')
->select('*')
->groupBy('day')
->get();
This is not working only showing one day and not grouping them , anyone knows why?
Thanks
This is a little bit complicated problem. I think, you are looking something like the following:
$schedules = DB::table('schedule')
->selectRaw('id, day, group_concat(name) as name')
->groupBy('day')
->get();
$data = array();
foreach ($schedules as $schedule) {
$data[$schedule->day][] = explode(",",$schedule->name);
}
var_dump($data);
output:
array (size=3)
'Mon' =>
array (size=1)
0 =>
array (size=12)
0 => string 'test' (length=4)
1 => string 'hello' (length=5)
'Tue' =>
array (size=1)
0 =>
array (size=3)
0 => string 'another' (length=6)
1 => string 'here' (length=4)
'Wed' =>
array (size=1)
0 =>
array (size=1)
0 => string 'go' (length=2)
Edit:
Do another group_concat for extra field.
$schedules = DB::table('schedule')
->selectRaw('id, day, group_concat(name) as name, group_concat(city) as city')
->groupBy('day')
->get();
$data = array();
foreach ($schedules as $schedule) {
$data[$schedule->day]['name'] = explode(",",$schedule->name);
$data[$schedule->day]['city'] = explode(",",$schedule->city);
}
var_dump($data);

Resources