0

I have an array from query like below:

array:84 [
      0 => array:2 [
        "comp" => "50007148"
        "cus" => "F0401"
      ]
      1 => array:2 [
        "comp" => "50007148"
        "cus" => "J0050"
      ]
      2 => array:2 [
        "comp" => "50007148"
        "cus" => "L"
      ]
      3 => array:2 [
        "comp" => "50007148"
        "cus" => "LT"
      ]
      4 => array:2 [
        "comp" => "50007148"
        "cus" => "RP"
      ]

Now I need to write a query where comp, cus in above query.

$rslt = Stdetl::whereIn('comp, cus', $categories)
                           ->where(YEAR(docdate), '=', 2019)
                           ->get(SUM(number))->toArray();

But this query is not working. I am getting error as follows:

(1/1) FatalErrorException
Call to undefined function App\Http\Controllers\YEAR()

But this is not the only mistake in that query .

2 Answers 2

1

YEAR() is a mysql function, you should use whereRaw() to query this. Like this:

->whereRaw('YEAR(docdate) = 2019')

Update:

For your whereIn() part, you have to make two queries, one for each column. So you will have to get the correct values from the array. This can be done using array_map.

For example:

->whereIn('comp', array_map(function($cat) { return $cat['comp']; }, $categories))
->whereIn('cus', array_map(function($cat) { return $cat['cus']; }, $categories))
Sign up to request clarification or add additional context in comments.

2 Comments

thank you .. I have error in whereIn('comp, cus', $categories) too even though there is comp and cus in table, it says Column not found: 1054 Unknown column 'comp, cus'
I have added an update for the whereIn part of the query.
0

You can't use whereIn like that, use this way

$rslt=Stdetl::whereIn('comp', $categories)
             ->whereIn('cus', $categories)
             ->where(date('Y', strtotime(docdate)), '=', 2019)
              ->get(SUM(number))->toArray();;

2 Comments

I tried like this but it take only first wherein only
use DB instead DB::table('table_name')->whereIn('comp', $categories) ->whereIn('cus', $categories)->where(date('Y', strtotime(docdate)), '=', 2019)->get(SUM(number))->toArray();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.