A
A
alexh78542020-03-05 13:52:07
Laravel
alexh7854, 2020-03-05 13:52:07

Why does laravel get data from the database if I loaded it into an array?

Hello.

Please tell me how to improve it.

I get a list of words with translations from a database. It turns out that all data from the database is loaded into the $myWords array .

$myWords = Word::with('translations')
            ->where('lang_id', $text_lang_id)
            ->whereHas('translations', function (Builder $query) use ($translate_to_lang_id) {
            $query->where('lang_id', '=', $translate_to_lang_id);
        })->get();

Then I want to retrieve the (hasMany) translation of the word. Why does laravel crawl into the database and get data from the database if I loaded all the data into the $myWords variable ?
$word = $myWords->where('word', $wordKey)->first();
$word->translations()->where('lang_id',$translateToLangId)->where('word_id', $word->id)->first()->translation;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2020-03-05
@alexh7854

You need to look at the documentation to understand the difference between $word->translations()and $word->translations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question