运行 Cron 作业调度时 Laravel 嵌套关系中的问题

问题描述 投票:0回答:1

我需要使用命令php artisan Schedule: run获取default_item_name关系的数据。但我没有得到 default_item_name 关系数据为空,因为 event_id 未从控制器传递。我的代码如下。

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();
        }
    }

查询菜单型号

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

菜单类别模型

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');
    }

我必须尝试下面的代码,但它不起作用,并且还使用了其他代码尝试但不起作用。请给我其他工作解决方案吗?

$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);
            }
        ]);
    }
]);
laravel eloquent cron scheduled-tasks eloquent-relationship
1个回答
0
投票

你可能想以 mysql 形式查看它

DB::enableQueryLog();
"your queries"
$queries = DB::getQueryLog()
dd($queries)
© www.soinside.com 2019 - 2024. All rights reserved.