mercredi 14 mars 2018

access query scope method in callback funcation

In Eloquent, I have little complex query, final query i want to achieve is this

     $form = makeQueryConditions
            ->with(array(
                'formFields' => function ($query) use ($dynamicQuestionsKeysAndValues) {

                        // duplicated Query logic
$query->duplicationed($dynamicQuestionsKeysAndValues);
                },
                'conditionalQuestions' => function ($query) use ($dynamicQuestionsKeysAndValues) {

                        //duplicated Query logic
$query->duplicationed($dynamicQuestionsKeysAndValues);

                }
            ))
            ->first();

in this query this logic is duplicated so i am to make query scope for this like this

    public function scopeDuplicationed($query ,$dynamicQuestionsKeysAndValues) 
{
                        $query->leftJoin('dynamic_questions_keys', 'dynamic_questions_keys.form_field_id', '=', 'form_fields.id')
                            ->leftJoin('dynamic_questions_key_values', 'dynamic_questions_keys.id', '=', 'dynamic_questions_key_values.dynamic_questions_key_id');
                        $query->where(function ($formFieldsIsDynamic) use ($dynamicQuestionsKeysAndValues) {
                            $formFieldsIsDynamic->where('form_fields.is_dynamic', '=', 0);
                            $formFieldsIsDynamic->orWhere(function ($formFieldsIsDynamicKeys) use ($dynamicQuestionsKeysAndValues) {
                                $formFieldsIsDynamicKeys->where('form_fields.is_dynamic', '=', 1);
                                $formFieldsIsDynamicKeys->where(function ($formFieldsIsDynamicKey_d) use ($dynamicQuestionsKeysAndValues) {
                                    $formFieldsIsDynamicKey_d->whereNull('dynamic_questions_keys.key');
                                    $formFieldsIsDynamicKey_d->orWhere(function ($formFieldsIsDynamicKey_dd) use ($dynamicQuestionsKeysAndValues) {
                                        foreach ($dynamicQuestionsKeysAndValues as $key => $value) {
                                            $formFieldsIsDynamicKey_dd->orWhere('dynamic_questions_keys.key', ($key));
                                            $formFieldsIsDynamicKey_dd->whereIn('dynamic_questions_key_values.values', ($value));
                                        }
                                    });
                                });
                            });
                        });
                        $query->select(DB::raw('DISTINCT(form_fields.id)'), 'form_fields.*');
}

when i try to access query scope method inside the callback function, it give error scopeDuplicationed method no found.Can any one help me please , how can i pass scopeDuplicationed method in call back function



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire