array in where condition in laravel - 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();

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();

Laravel 5.8 - collect method return me undefined index

I got this array:
What I want to do next is to make a collection from the array and I write:
$variant_images = collect($p->images);
but I got the error:
"Undefined index: images"
What's bad in my code and how to solve it?
Assuming $p is array like this
$p = [
'images' => [
1, 2, 3, 4
]
];
you can write
$collection = collect($p);
// access the images
var_dump($collection->get('images'));
and the output would be
array:4 [▼
0 => 1
1 => 2
2 => 3
3 => 4
]

How to sort two array value from lowest to highest and store in database

This is my array data from
$users = [1, 2, 3, 4];
$bidder = [3, 6, 7, 2];
And i want to sort like this
users_id | bidder
---------- ** ----------
4 | 2
1 | 3
2 | 6
3 | 7
and In Controller,using Collections I sort in this way
$users=$request->user_id;
$bidder=$request->bidder_rate;
$data = collect($users)->map(function ($user, $key) use ($bidder) {
return [
'user_id' => $user,
'bidder' => $bidder[$key]
];
})->sortBy('bidder');
Now when i do dd($data).It worked
1 => array:2 [▼
"user_id" => "2"
"bidder" => "10"
]
0 => array:2 [▼
"user_id" => "1"
"bidder" => "12"
]
2 => array:2 [▼
"user_id" => "4"
"bidder" => "15"
]
But now i want to save all user_id and bidder sorted data in respective field(user_id & bidder) in
array form.So how can i store them
$auction=new Auction();
$auction->name=$data['auction_name'];
$auction->user_id=$data['user_id']; //I think i cannot do like this
$auction->bidder=$data['bidder'];
Try this
$auction=new Auction();
$auction->name=collect($data)->pluck("user_id");
$auction->user_id=collect($data)->pluck("bidder");
$auction->bidder=$data['bidder'];

Adding missing rows of data in a collection - unexplanable Laravel behavior

I have a table of data and I need to fetch an array from it, that looks like this:
[
['Mon', 25],
['Tue', 13],
['Thu', 25]
]
I'm achieving this through some collection acrobatics. At some point I am mapping the collection adding the numerical value of that day (1 Monday, 2 Tuesday) as a key, so I can sortKeys() later.
The problem is not all days are always present and I want to add them with a value of 0 at their respective place.
My first attempt was foreach on an array of days of the week, and if
$collection->flatten()->search($day) returns false, prepend that day. This works fine, but Thu always get appended. It never returns true on the search even though it's copied and pasted and should be identical. All other days are skipped/prepended correctly...
Then I tried array_search on toArray() and the same thing happened. Thu never returns true ...
This is extremely weird, basically Thu == Thu returns false
Is there anyway I can use array_merge or something like that to make it better (or get it working at all?).
This is one way of doing it. It could be done much cleaner, if your data was structured different in the first place, like using day name/value as key, instead of having both in sub arrays, but I will keep to the original question:
$defaultDays = collect([
['Mon', 0],
['Tue', 0],
['Wed', 0],
['Thu', 0],
['Fri', 0],
['Sat', 0],
['Sun', 0],
]);
$days = [
['Mon', 25],
['Tue', 13],
['Thu', 25]
];
// Use $defaultDays to map, as we will need all seven days no matter what.
$days = $defaultDays->map(static function (array $defaultDay) use ($days) {
// Current match is the default day...
$match = $defaultDay;
foreach ($days as $day) {
if ($day[0] === $defaultDay[0]) {
$match = $day;
}
}
return $match;
});
This will result in:
Illuminate\Support\Collection {#1388
#items: array:7 [
0 => array:2 [
0 => "Mon"
1 => 25
]
1 => array:2 [
0 => "Tue"
1 => 13
]
2 => array:2 [
0 => "Wed"
1 => 0
]
3 => array:2 [
0 => "Thu"
1 => 25
]
4 => array:2 [
0 => "Fri"
1 => 0
]
5 => array:2 [
0 => "Sat"
1 => 0
]
6 => array:2 [
0 => "Sun"
1 => 0
]
]
}

laravel Maatwebsite validate sum multi column

hi im using Maatwebsite with laravel everything working so good so far ..
but i want to update excel like this ..
number date amount
1 2020-01-01 10
1 2020-01-01 -5
1 2020-01-01 -5
2 2020-02-02 20
2 2020-02-02 -20
3 2020-03-03 50
3 2020-03-03 -50
3 2020-03-03 40
3 2020-03-03 -40
what i want thats the sum of the amount with number 1 sould return 0 or end the import
number date amount
1 2020-01-01 10
1 2020-01-01 -5
1 2020-01-01 -5
how can i check this (10 + -5 + -5 == 0) or fail
and for 2 and 3 ect ..
thanks a lot
I explained all the steps in comments but I will explain here as well.
When you loop through each row, you first need to grab all the rows where number field is 1, 2 or 3, what ever the number is currently in the iteration.
In this case, the first loop results will be:
number date amount
1 2020-01-01 10
1 2020-01-01 -5
1 2020-01-01 -5
Then you sum (add up) the values from the amount column.
In this case, it will sum like this:
10
-5
-5
-----
0
If the answer is not 0, take the current sheet ($sheet) and return what you currently have, to be exported.
Full class:
...
use Maatwebsite\Excel\Concerns\FromArray;
class ExampleExport implements FromArray
{
public function array() : array
{
// EXAMPLE hardcoded data for the purpose of this demo
$rows = [
[
'number' => 1,
'date' => '2020-01-01',
'amount' => 10
],
[
'number' => 1,
'date' => '2020-01-01',
'amount' => -5
],
[
'number' => 1,
'date' => '2020-01-01',
'amount' => -5
],
[
'number' => 2,
'date' => '2020-02-02',
'amount' => 20
],
[
'number' => 2,
'date' => '2020-02-02',
'amount' => -20 // <-------- Change this to -21 to test
],
[
'number' => 3,
'date' => '2020-03-03',
'amount' => 50
],
[
'number' => 3,
'date' => '2020-03-03',
'amount' => -50
],
[
'number' => 3,
'date' => '2020-03-03',
'amount' => 40
],
[
'number' => 3,
'date' => '2020-03-03',
'amount' => -40
],
];
// EXAMPLE hardcoded data for the purpose of this demo
$rows = collect($rows);
// You probably going to get this from your database so should look like this:
// $rows = \App\Model::all(['number', 'date', 'amount']); // OR below
// $rows = DB::table('examples')->select(['number', 'date', 'amount'])->get();
// Blank sheet
$sheet = [];
foreach ($rows as $row) {
// Get all the rows where the `number` field has the same value,
// for example 1, 2, or 3, then sum the `amount` field (add up).
//
// If the amount does not add up to 0, stop and return what you currently got
if ($rows->where('number', $row['number'])->sum('amount') !== 0) {
return $sheet;
}
// Else, add them to the sheet and continue.
$sheet[] = $row;
}
return $sheet;
}
}
Results:
When amount adds up to 0:
When amount does not add up to 0:

Resources