skip to Main Content

I need to get data of default_item_name relationship by using command php artisan schedule: run. but I am not getting default_item_name relationship data is empty because event_id not passed from controller. My code is Below.

CronController

public function sendFoodMenuPdfEmailOrTelegram()
    {
         foreach ($getEnquiryEvents as $key => $value) {
              $FoodPackageMenu = EnquiryMenu::with(['foodPackage', 'menu_category_without_item.default_item_name'])
            ->where('food_package_id', $food_template_id)
                ->where('enquiry_id', $enquiry_id)
                ->whereNull('deleted_at')
                ->where('event_id', $event_id)
                ->groupBy('menu_category_id')
                ->orderBy('id', "ASC")
                ->get()
                ->toArray();
        }
    }

EnquiryMenu Model

public function menu_category_without_item()
    {
        return  $this->hasOne(MenuCategory::class,'id','menu_category_id');
    }

MenuCategory Model

public function default_item_name($eventid = null)
    {
        $eventid = $eventid ?? request()->get('event_id');
        return $this->hasMany(EnquiryMenu::class, 'menu_category_id', 'id')
                ->select('snc_menu_items.*',
                    DB::raw("IF(snc_enquiry_menus.item_id = '0',snc_enquiry_menus.menu_category_id, snc_enquiry_menus.menu_category_id) as menu_category_id", ""), 
                    'snc_enquiry_menus.extra_menu','snc_enquiry_menus.extra_charge','snc_enquiry_menus.extra_charge_amount','snc_enquiry_menus.service_time','snc_enquiry_menus.food_service_time','snc_enquiry_menus.menu_status','snc_enquiry_menus.is_complimentory','snc_enquiry_menus.is_complimentory', DB::raw('IFNULL(snc_enquiry_menus.notes, "") as notes'),DB::raw('IFNULL(snc_enquiry_menus.description, "") as description'))
                ->leftJoin('snc_menu_items','snc_menu_items.id', '=','snc_enquiry_menus.item_id')
                ->where('snc_enquiry_menus.event_id', $eventid)
                ->whereNull('snc_enquiry_menus.deleted_at');
    }

I have to try Below code but it’s not working and also used other code try but not working. Please give me other solution for work me?

$FoodPackageMenu = EnquiryMenu::with([
    'foodPackage', 
    'menu_category_without_item' => function($q) use ($event_id) {
        $q->with([
            'default_item_name' => function($qe) use ($event_id) {
                $qe->yourFunctionName($arg1, $event_id);
            }
        ]);
    }
]);

2

Answers


  1. You might want to look at it in mysql form

    DB::enableQueryLog();
    "your queries"
    $queries = DB::getQueryLog()
    dd($queries)
    
    Login or Signup to reply.
  2. The $eventid can not passed default_item_name model function the default_item_name written as:

    public function default_item_name()
    {
        return $this->hasMany(EnquiryMenu::class, 'menu_category_id', 'id');
    }
    

    In your controller default_item_name callback function written the remaining query by passing $eventid

    $FoodPackageMenu = EnquiryMenu::with([
        'foodPackage', 
        'menu_category_without_item' => function($q) use ($event_id) {
            $q->with([
                'default_item_name' => function($qe) use ($event_id) {
                    $qe->select(
                    'snc_menu_items.*',
                    DB::raw("IF(snc_enquiry_menus.item_id = '0',snc_enquiry_menus.menu_category_id, snc_enquiry_menus.menu_category_id) as menu_category_id", ""), 
                    'snc_enquiry_menus.extra_menu',
                    'snc_enquiry_menus.extra_charge',
                    'snc_enquiry_menus.extra_charge_amount',
                    'snc_enquiry_menus.service_time',
                    'snc_enquiry_menus.food_service_time',
                    'snc_enquiry_menus.menu_status',
                    'snc_enquiry_menus.is_complimentory',
                    'snc_enquiry_menus.is_complimentory', 
                    DB::raw('IFNULL(snc_enquiry_menus.notes, "") as notes'),
                    DB::raw('IFNULL(snc_enquiry_menus.description, "") as description'))
                    ->leftJoin('snc_menu_items','snc_menu_items.id', '=','snc_enquiry_menus.item_id')
                    ->where('snc_enquiry_menus.event_id', $event_id)
                    ->whereNull('snc_enquiry_menus.deleted_at');
                }
            ]);
        }
    ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search