Get null if not found in the database Laravel - laravel

I have a problem with Eloquent, my query is:
$b = Dictionary::find($a,$LangtoTranslate);
The problem is count($a) is 67, count($b) is 64..can I get null if value is not founded? In my case, the missing row in $a are "" but I need to have null or "" if not I lost syncronism.
The $a is a simple array:
array:67 [▼
0 => ""
1 => "Frase fatale non identificato"
2 => "Indice fuori dei limiti"
3 => "Errata configurazione del componente"
4 => "Raggiunto il numero massimo di annidamenti"
5 => "Id sequenza non identificata"
6 => "Rassegnazione"
7 => "Salto ad uno step coesistente"
8 => "RETURN senza passare dal via"
9 => "Da Implementare"
The DB is made just from language columns:
it- IT
en-GB
de-DE
Other Languages
Italiano
English
Deutsch
i've tried olso:
$b = Dictionary::where($reflang,$a)->first($LangtoTranslate);
But I get null
For reference:
$reflang = 'it-IT';
$LangtoTranslate = [ array of languages ]; => ex: ['en-GB','de-DE']

As it is unclear what you want to achieve since you only provide partial code I will have my own version of your code.
Instead of using find() you want to use the where() function.
$a = ["0","1","2"];
$b = [];
foreach($a as $text){
$found = Dictionary::where('YourColumn','=',$text)->first();
if($found !== null){
array_push($b,$found->YourColumn);
}else{
array_push($b, null);
}
}

Related

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

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

How to add array values to another array

I have two sets of array A and B, array A and B on submission is supposed to enter a database table.
A(result)
array:2 [
0 => 7
1 => 8
]
B
array:3 [
"student_test_id" => 8
"question_id" => 4
"test_id" => 3
]
EXPECTED RESULT
On submit, i want the values of Array A and B to enter this table like this
id| student_test_id | test_id | question_id |result
1 | 8 | 3 | 4 | 7
2 | 8 | 3 | 4 | 8
WHAT I HAVE TRIED
$result = $request->result;
$array_conv = array(
"student_test_id"=>$request->student_test_id,
"question_id"=>$request->question_id,
"test_id"=>$request->test_id,
);
foreach($request->result as $result){
$test_log = new StudentTestLog();
$test_log->student_test_id = $array_conv["student_test_id"];
$test_log->question_id = $array_conv["question_id"];
$test_log->test_id = $array_conv["test_id"];
$test_log->result = $result['id'];
$test_log->save();
if($test_log->save()){
return response()->json('Submited', 200);
}else{
return response()->json('Error Submitting', 400);
}
}
Please assist Thank you
When returning response from foreach except first all other iterations of loop are omitted as return ends method execution. You should move return statements outside the loop. Try something like this:
$array_conv = array(
"student_test_id"=>$request->student_test_id,
"question_id"=>$request->question_id,
"test_id"=>$request->test_id,
);
$data_to_persist = [];
foreach($request->result as $result){
array_push( $data_to_persist, [
'student_test_id' => $array_conv["student_test_id"],
'question_id' => $array_conv["question_id"],
'test_id' => $array_conv["test_id"],
'result' => $result['id']
]);
}
$saved = StudentTestLog::insert( $data_to_persist );
if($saved){
return response()->json('Submited', 200);
}else{
return response()->json('Error Submitting', 400);
}

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.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
]

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

Resources