app/Http/Controllers/EventController.php
/**
     * Create multiple event based on date range.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Create multiple event based on date range.
Create multiple event based on date range .
public function createMultipleEvent(Request $request)
    {
        $events = [];
        $start    = new DateTime($request->input('dateFrom'));
        $start->modify('first day of this month');
        $end      = new DateTime($request->input('dateTo'));
        $end->modify('first day of next month');
        $interval = DateInterval::createFromDateString('1 month');
        $period   = new DatePeriod($start, $interval, $end);
        Event::whereDate('edate', '>=', $request->input('dateFrom'))
            ->WhereDate('edate', '<=', $request->input('dateTo'))
            ->delete();
        foreach ($period as $dt) {
            foreach ($request->input('days') as $key => $day) {
                $date = new DateTime('first '.$day.' of '.$dt->format("Y-m"));
                $thisMonth = $date->format('m');
                while ($date->format('m') === $thisMonth) {
                    if(
                        strtotime($date->format('Y-m-d')) >= strtotime($request->input('dateFrom')) &&
                        strtotime($date->format('Y-m-d')) <= strtotime($request->input('dateTo'))
                        ) {
                            array_push($events, [
                                "title" => $request->input('title'),
                                "edate" => $date->format('Y-m-d'),
                                "created_at" => date('Y-m-d H:i:s'),
                                "updated_at" => date('Y-m-d H:i:s')
                            ]);
                        }
                    $date->modify('next '.$day);
                }
            }
        }
        Event::insert($events);
        return EventResource::collection(
            Event::whereMonth('edate', intval($request->input('curMon')))
                ->whereYear('edate', $request->input('curYear'))
                ->get()
        );
    }
public function createMultipleEvent ( Request $ request ) { $ events = [ ] ; $ start = new DateTime ( $ request -> input ( 'dateFrom' ) ) ; $ start -> modify ( 'first day of this month' ) ; $ end = new DateTime ( $ request -> input ( 'dateTo' ) ) ; $ end -> modify ( 'first day of next month' ) ; $ interval = DateInterval :: createFromDateString ( '1 month' ) ; $ period = new DatePeriod ( $ start , $ interval , $ end ) ; Event :: whereDate ( 'edate' , '>=' , $ request -> input ( 'dateFrom' ) ) -> WhereDate ( 'edate' , '<=' , $ request -> input ( 'dateTo' ) ) -> delete ( ) ; foreach ( $ period as $ dt ) { foreach ( $ request -> input ( 'days' ) as $ key => $ day ) { $ date = new DateTime ( 'first ' . $ day . ' of ' . $ dt -> format ( " Y-m " ) ) ; $ thisMonth = $ date -> format ( 'm' ) ; while ( $ date -> format ( 'm' ) === $ thisMonth ) { if ( strtotime ( $ date -> format ( 'Y-m-d' ) ) >= strtotime ( $ request -> input ( 'dateFrom' ) ) && strtotime ( $ date -> format ( 'Y-m-d' ) ) <= strtotime ( $ request -> input ( 'dateTo' ) ) ) { array_push ( $ events , [ " title " => $ request -> input ( 'title' ) , " edate " => $ date -> format ( 'Y-m-d' ) , " created_at " => date ( 'Y-m-d H:i:s' ) , " updated_at " => date ( 'Y-m-d H:i:s' ) ] ) ; } $ date -> modify ( 'next ' . $ day ) ; } } } Event :: insert ( $ events ) ; return EventResource :: collection ( Event :: whereMonth ( 'edate' , intval ( $ request -> input ( 'curMon' ) ) ) -> whereYear ( 'edate' , $ request -> input ( 'curYear' ) ) -> get ( ) ) ; }
Create multiple event based on date range.
Create multiple event based on date range .
// Remove existing event based on date range