Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to WhereIn a whereday of array in laravel

Tags:

php

laravel

array:23 [▼
      0 => 1
      1 => 2
      2 => 3
      3 => 4
      4 => 5
      5 => 8
      6 => 9
      7 => 10
      8 => 11
      9 => 12
      10 => 15
      11 => 16
      12 => 17
      13 => 18
      14 => 19
      15 => 22
      16 => 23
      17 => 24
      18 => 25
      19 => 26
      20 => 29
      21 => 30
      22 => 31
    ]

this is a array of working days apart from Sunday and Saturday and i have a table of months data and i need a Laravel where condition for comparing all data with whe

->whereYear('Clock_Day',$yearofdata)
->whereMonth('Clock_Day',$monthofdata)
->whereIn('Clock_Day','=',$workdays) //here can i use something like whereIn->whereday---for comparing all array values and get as per the data
like image 561
Madhu Nair Avatar asked Jan 31 '18 11:01

Madhu Nair


2 Answers

There is no whereDayIn() or similar method, but you can do this:

->whereYear('Clock_Day', $yearofdata)
->whereMonth('Clock_Day', $monthofdata)
->where(function($q) use($workdays) {
    foreach ($workdays as $day) {
        $q->whereDay('Clock_Day', '=', $day, 'or');
    }
})
like image 55
Alexey Mezenin Avatar answered Oct 30 '22 18:10

Alexey Mezenin


If Clock_Day is a field in your table, you will need to extract the date part you are comparing for each piece (Year, Month, Day). You could possibly use whereIn with DB::raw:

->where(DB::raw('YEAR("Clock_Day")'),$yearofdata)
->where(DB::raw('MONTH("Clock_Day")'),$monthofdata)
->whereIn(DB::raw('DAYOFMONTH("Clock_Day")'),$workdays)

You would only use whereYear or whereMonth to compare those values against fields in your table called 'year' and 'month', but since you want to use the same 'Clock_Day' field for all comparisons you need to extract the relevant data for each part.

like image 2
davidethell Avatar answered Oct 30 '22 18:10

davidethell