vendredi 22 juin 2018

refactor code to be database-agnostic

Actually I have two questions:

1) Is it possible to configure Laravel temporarily for a call to be database-agnostic and work on models instantiated in code only? In other words to prevent any database access when working with models and relations.

2) I tried to refactor this code to allow 1 but I'm hitting problems

/** @var Collection $collection */
    $collection = $model->relation()->whereIn('MY_ID', [
        RelationModel::IDENTIFIER_FOO,
        RelationModel::IDENTIFIER_BAR,
        // ...
    ])->get();
    if (0 === $collection->count()) {
        throw new \InvalidArgumentException('no entry found but mandatory');
}

Which I tried to change to this:

/** @var Collection $collection */
    // note the missing parentheses
    $collection = $model->relation->whereIn('MY_ID', [
        RelationModel::IDENTIFIER_FOO,
        RelationModel::IDENTIFIER_BAR,
        // ...
    ])->get();
    if (0 === $collection->count()) {
        throw new \InvalidArgumentException('no entry found but mandatory');
}

But now I'm getting the error

Type error: Too few arguments to function Illuminate\Support\Collection::get(), 0 passed

It seems omitting the parentheses there is a different type of collection returned which requires to pass a parameter instead of none for the other type of collection.

Is it possible to refactor this to allow both kinds of requests, with and without database access?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2MectNf
via IFTTT

Aucun commentaire:

Enregistrer un commentaire