E
E
Eugene2018-02-20 12:19:21
PHP
Eugene, 2018-02-20 12:19:21

Laravel: How to pass value to condition when grouping SQL query parameters?

Tell me how to correctly pass a value / variable to a group of conditions when grouping parameters ?

// Грубо говоря мне нужно получить такой запрос:
select id from `table` where `order_status ` = ? and (`event_id ` = ? or `event_depend1 ` = ? or `event_depend2 ` = ?)

But when grouping, it swears at the absence of $varName
$varName = 'value';
$ordersData = OrderEAG::select('id')
                    ->where('order_status', $statusActive)
                    ->where(function ($query) {
                        $query->orWhere('event_id', $varName)
                            ->orWhere('event_depend1', $varName)
                            ->orWhere('event_depend2', $varName);
                    })
                    ->get();

// Undefined variable: varName

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2018-02-20
@atachrus

php.net/manual/en/functions.anonymous.php
Example #3 Inheriting variables from the parent scope

D
Dmitry Chekanov, 2018-02-20
@LanDer931

$varName = 'value';
$ordersData = OrderEAG::select('id')
    ->where('order_status', $statusActive)
    ->where(function ($query) use($varName) {
        $query->orWhere('event_id', $varName)
           ->orWhere('event_depend1', $varName)
           ->orWhere('event_depend2', $varName);
       })
   ->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question