mardi 9 juillet 2019

Laravel - Eloquent "relation" submit query only one time

I noticed a strange behaviour using eloquent relaration.

I wrote this simple test:

Route::get('/myTest', function () {
    $book = \App\Models\BookModel::findOrFail(7);

    $count_start = 1;
    $couny_end   = 3;
    $position    = false
    while ($count_start <= $count_end && $position == false) {
        try {
            $position = $book->position->name;
        } catch (Exception $e) {
            \Log::info('  book not found, waiting...');
        }
        \Log::info('  position:'.$position);
        sleep(5);
        $count_start++;
    }    
});

the position Table is filled from an external process then, the while is used to wait that a position is set.

The strange behaviour is that the first time the process enter in the while, the query is correctly execute:

"select * from `position` where `position`.`fk_book` = ? limit 1","bindings":[7]

if the previous query return null, the second times the query is not executed again and I do not understand why!

Is this normal? Why the relation query is executed only first time?

Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire