Get null if not found in the database Laravel

Viewed 293

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']
1 Answers

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