Trying to get Values between 2 dates but need start date and end date should be as given instead of as per database - laravel

I am trying to get data between "Start" and "End" dates from database which is working fine and query giving me the result, Say
Start = 09-02-2022.
End = 10-03-2022.
Database values start = "01-03-2022 to 09-03-2022" so i am getting result from date say 16-02 to 01-03.
But i want result should start with 09-02 and end with 10-03. If value is not present in database then it should give as "0"
Check attached Image for reference.
Code for Getting dates for last 30 days -
$getalldates = array();
for($d = (Carbon::today()->subDays(30)); $d->lte(carbon::today()); $d->addDay())
{ $getalldates[] = $d->format('Y-m-d');}
Code for Getting data from database
$rahul = Buffalomilkrecord::select(DB::raw('date'), DB::raw('sum(totalmilk) as totalmilk, DATE_FORMAT(date,"%d") as "daykey"'))
->whereBetween('date', [$olddate, $todaydate] )
->groupBy(DB::raw('date'))
->orderBy('date','asc')
->get()->pluck('date')->toArray();
Thanks in Advance.
This is what i tried
$result = array();
foreach($getalldates as $date){
$totalmilk= Buffalomilkrecord::select(DB::raw('sum(totalmilk) as totalmilk'))->whereDate('date',$date)->pluck('totalmilk');
if($totalmilk){
$result[] = array($date =>$totalmilk);
} else {
$result[] = array($date =>0);
}
}
I am expecting
This is my output 0 => array:1 [▼ "2022-02-09" => Illuminate\Support\Collection {#348 ▼ #items: array:1 [▼ 0 => null ] }
and i am expecting "2022-02-09" = "0"
Check Image

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

how to save array data coming from view in laravel

This the data when user submit the form :
POST Data
_token
"JNDt8WC6kVbvrSdFTKSGnHsfzTuIsbthslf5Gqjs"
invoice_number
"15"
dateofbill
"2019-04-19"
customer_name
"praveen kumar tiwari"
customer_mobile
"8924001750"
sno
array:3 [▼
0 => "1"
1 => "2"
2 => "3"
]
item_name
array:3 [▼
0 => "jeans"
1 => "shirt"
2 => "lower"
]
qty
array:3 [▼
0 => "2"
1 => "3"
2 => "2"
]
price
array:3 [▼
0 => "20000"
1 => "232"
2 => "12"
]
gst
array:3 [▼
0 => "1200"
1 => "22"
2 => "12"
]
discount
array:3 [▼
0 => "100"
1 => "23"
2 => "12"
]
textarea
""
i cannot be able to store this data into a table. i am trying with for loop but getting an error "Undefined offset: 3".
Code inside the controller
for($i=0;$i<=count($request['sno']);$i++)
{
$invoice = new Invoice;
$invoice->sendbill_id=$bill->id;
$invoice->sno=$request['sno'][$i];
$invoice->item_name=$request->item_name[$i];
$invoice->qty=$request->qty[$i];
$invoice->price=$request->price[$i];
$invoice->gst=$request->gst[$i];
$invoice->discount=$request->discount[$i];
$invoice->save();
}
i want to store these 3 values comming in the array form (sno,item_name,qty,price,gst,discount) in 3 diffrent rows
You should try to use laravel eloquent to save it. Here is some example that you can check it out. Laravel : Many to many insertion
The problem you have is indeed your loop: for($i=0;$i<=count($request['sno']);$i++).
To be specific it is this right here <=:
$i<=count()
^^
Take a look at your array:
[
0 => "1"
1 => "2"
2 => "3"
]
You got a total of 3 objects. count($request['sno']) will therefore return 3 since the count() function does not start counting at 0!
However, calling an index (e.g. $request['sno'][1]) will not return the first object (0 => "1") but the second (1 => "2"). I think you see where I am going.
Since the loop will go on until $i equals 3 the loop will be completed 4 times. At the last time (where $i == 3) you try to get the 4th item out of your array which does not exist so an error message pops up: Undefined offset: 3.
To solve this just change this
$i<=count()
^^
to <. The loop will only be executed if $i is still smaller then 3. This is the case if $i == 2. No error message will pop up.
I do not want to attack or hurt you in any way, but it seems to me that you are relatively new to PHP. Of course, that's not a shame, but I'm wondering if a huge framework like Laravel is right for you. First the basics, then comes the advanced.
But that's only as a small comment and tip from me.

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!

Calculate a number from two variables

I'm trying to complete a mathematical operation with two variables like this :
$cv_moteur = $request->input('cv_moteur');
$distance = $request->input('nm_distance');
if($cv_moteur === '7CV'){
$taux = '0,401';
$rencontre_officiel->taux = $taux;
$rencontre_officiel->mt_indemnite_km = $taux * $distance;
but when i debug i get $rencontre_officiel->mt_indemnite_km = 0
Why ? i should get : 40,1 . Someone know why i get 0 ?
Here is the result of the debug :
#attributes: array:10 [▼
"rencontre_id" => 38
"licencie_id" => "125"
"fonction_officiel_id" => "1"
"bareme_id" => "1"
"nm_distance" => "100"
"mt_prime" => "175.00"
"cv_moteur" => "7CV"
"taux" => "0,401"
"mt_indemnite_km" => 0
"statut_officiel" => ""
Thanks a lot in advance.
In your case, $taux = "0,401"; is treated as a String. Attempting to convert this to a number using a method like floatval($taux) will result in 0, as using "," as a decimal point is invalid in calculations (valid in language though).
What you need to do is convert the "," to a ".":
$taux = floatval(str_replace(",",".", $taux));
Following that, you should be able to call:
$rencontre_officiel->mt_indemnite_km = $taux * floatval($distance);

Getting Error Message For oci_execute() Error (PHP)

I would like to get the specific error message if a query fails in Oracle 10g. For MySQL, PHP has the mysql_error() function that can return details about why a query failed. I check the php.net manual for the oci_execute() function, but from what I see it only returns false on fail.
I tried using oc_error(), but I am not getting anything from it.
Here is a code sample:
$err = array();
$e = 0;
//Cycle through all files and insert new records into database
for($f=0; $f<sizeof($files); $f++)
{
$invoice_number = $files[$f]['invoice_number'];
$sold_to = $files[$f]['sold_to'];
$date = $files[$f]['date'];
$sql = "insert into invoice (dealer_id, invoice_number, invoice_date)
values ('$sold_to', '$invoice_number', '$date')";
$stid = oci_parse($conn, $sql);
$result = oci_execute($stid);
//If query fails
if(!$result)
{
$err[$e] = oci_error();
$e++;
}
}
print_r($err);
Response for print_r($err):
Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => )
Have you tried to pass $stid to oci_error?
$err[$e] = oci_error($stid);
The oci_error() function takes an argument that specifies the context of your error. Passing it no argument will only work for certain kinds of connection errors. What you want to do is pass the parsed statement resource, like so:
$err = oci_error($stid);
(Note also that the return value from oci_error is an array, so I assigned the entire output to your $err array variable)

Resources