类型错误:传递给App \ Events :: __ construct()的参数1必须是App \ Events的实例,给出Illuminate \ Support \ Collection的实例

时间:2018-03-21 04:12:01

标签: email events laravel-5 listener

我想使用事件和监听器在laravel 5.5中发送电子邮件。但是当在控制器上调用Event时,我收到了此错误消息。

类型错误:传递给App \ Events \ EMailNotif \ UserWorkflowEmail :: __ construct()的参数1必须是App \ Events \ EMailNotif \ App \ WFApprover的实例,给定的Illuminate \ Support \ Collection实例,在C中调用:第289行\ wamp64 \ www \ B2BSite \ app \ Http \ Controllers \ OrdersController.php

我在网上搜索,但无法找到解决方案。我希望我能在这个论坛找到解决方案。请帮助。

这是我的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Session;
use Auth;
use Excel; 
use App\TrxRekap;
use App\TrxDetails;
use App\WFApprover;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Input;
use App\Events\EmailNotif\UserWorkflowEmail;
//use app/TrxFleets;


class OrdersController extends Controller
{

    protected $redirectTo = '/OrdersController';

     /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function upload_orders(){
        return view('orders.upload_order');
    }

    public function show_fleets($dlvdate){
        print_r('In Here ..');
        return view('orders.fleet_details');
    }

    public function save_orders(Request $request){

        $cust_info = Session::get('customer_info');
        $trx_rekap = Session::get('trx_rkp');
        $trxDetails = Session::get('trx_detail');
        $periode = Session::get('periode_cmo');
        $DetailFleet = Session::get('DetailFleet');

        foreach($cust_info as $key => $value) {
            $cust_ship_id =  $value->cust_ship_id;
            $cust_bill_id =  $value->cust_ship_id;
            $customer_number =  $value->customer_number;
            $subregion =  $value->subregion;
            //$rsdh_id  = $value->rsdh_id;
            //$asdh_id  = $value->asdh_id;
        } 

        $no_cmo = $this->getOrderNumber($customer_number);

        $delivery_date_1 ="";
        $delivery_date_2 ="";
        $delivery_date_3 ="";
        $delivery_date_4 ="";

        foreach($trx_rekap as $value) {
            if ($key ==0 ) {
                $periode_cmo = $value['periode_cmo'];
            }
            if ($value['num']== '1') {
                $delivery_date_1 = $value['datedlv'];
            }

            if ($value['num'] == '2') {
                $delivery_date_2 = $value['datedlv'];
            }

            if ($value['num'] == '3') {
                $delivery_date_3 = $value['datedlv'];
            }

            if ($value['num'] == '4') {
                $delivery_date_4 = $value['datedlv'];
            }
        }

        $save_rekap = [];
        $cmo_number = $this->getOrderNumber($customer_number);
        $header_id = DB::select("select nextval('trx_cmo_headers_id_seq') as header_id");


        foreach($header_id as  $value) {
            $id = $value->header_id;
        }

        $save_rekap = [
              'header_id' => $id,
              'cust_ship_id' => $cust_ship_id,
              'cust_bill_id' => $cust_bill_id,
              'customer_number' => $customer_number,
              'cmo_number' => $cmo_number,
              'periode_cmo' => $periode_cmo,
              'subregion' => $subregion,
              'cmo_status' => 'CMO',
              'rsdh_id' => null,
              'asdh_id'=> null,
              'delivery_date_1' => $delivery_date_1,
              'delivery_date_2' => $delivery_date_2,
              'delivery_date_3' => $delivery_date_3,
              'delivery_date_4' => $delivery_date_4,
              'created_by' => Auth::user()->name,
              'updated_by' => Auth::user()->name
        ];

        DB::begintransaction(); 

        DB::table('trx_cmo_headers')->insert(array($save_rekap));

        $save_detail[]='';

        //Save Order Detail
        $i=1;
        foreach ($trxDetails as $value) {

            $save_detail = [
             'header_id' => $id,
             'kode_item' => $value['kode_item'],
             'kode_descr' => $value['kode_descr'],
             'uom_trx' => $value['uom_trx'],
             'stock_awal_cycle' => $value['stock_awal_cycle'],
             'sp' => $value['sp'],
             'total_stock' => $value['total_stock'],
             'est_sales' => $value['est_sales'],
             'est_stock_akhir' => $value['est_stock_akhir'],
             'est_sales_nextmonth' => $value['est_sales_nextmonth'],
             'buffer' => $value['buffer'],
             'average' => $value['average'],
             'doi' => $value['doi'],
             'cmo' => $value['cmo'],
             'cmo_status' => 'CMO',
             'delivery_date' => $value['delivery_date'],
             'delivery_num' => $value['delivery_num'],
             'delivery_qty' => $value['delivery_qty'],
             'price_crt' => doubleval($value['price_crt']),
             'disc_percent' => doubleval($value['disc_percent']),
             'disc_value' => $value['disc_value'],
             'netto' => $value['netto'],
             'ppn' => $value['ppn'],
             'extended_price' => $value['extended_price'],
             'created_by' => Auth::user()->id,
             'updated_by' => Auth::user()->id];

             DB::table('trx_cmo_details')->insert(array($save_detail));

             $i=$i+1;
             $save_detail[] ="";
        }

        //Save Fleet Detail
        $i=1;
        foreach ($DetailFleet as $value) {

            $fleet_detail = [ 
              'cmo_header_id' => $id,
              'dlv_date' => $value['delivery_date'] ,
              'fleet_no' => $value['Fleetke'],
              'fleet_type' => $value['Fleet'],
              'kode_item' => $value['kode_descr'],
              'dlv_qty'  => $value['delivery_qty'],
              'dlv_price' => doubleval($value['price_crt']),
              'dlv_disc' => doubleval($value['disc_value']),
              'dlv_netto' => doubleval($value['netto']),
              'dlv_extended' => doubleval($value['extended_price']),
              'created_by' => Auth::user()->id,
              'updated_by' => Auth::user()->id];

             DB::table('trx_cmo_fleets')->insert(array($fleet_detail));

             $i=$i+1;
             $fleet_detail[] ="";
        }

        DB::commit();

        $Success = "CMO No. " . $cmo_number . ' Berhasil di Simpan!';


        //*** Send Email With Attachment
        //** Get User Info
        $userInfo = Session::get('user_info');
        foreach ($userInfo as $key => $value) {
            $person_id = $value->person_id;
            $position = $value->position_id;
        }

        $subject = 'Order Number ' . $cmo_number . ' ' . 'Membutuhkan Approval Anda'; 
        //Get hirarki id
        $hirarki_id = DB::table('mtl_hirarki')
                      ->where('hirarki_name', '=', 'CMO APPROVAL')
                      ->get();

        foreach ($hirarki_id as $key => $value) {
            $idhirarki = $value->id;
        }
        //Set workflow First
        //person_id, hirarki_id, position_id, cust_ship_id, subject
        $wf_check = DB::select('Select * From insert_approvers(?,?,?,?,?)',[$person_id, $idhirarki,$position,$cust_ship_id,$subject]);

        foreach ($wf_check as $key => $value) {
            $wf_key = $value->wf_keyid;
        }


        if(!empty($wf_check)) {

          //Get Email Notif
          $wfApprover = DB::table('wf_approval')
                        ->where('sender_pos_id', '=', $position)
                        ->where('wf_key_id', '=', $wf_key)
                        ->where('status', '=','NULL')
                        ->get();

           event(new UserWorkflowEmail($wfApprover));
         }

        Session::flash('success', $Success); 

        return redirect('upload_excel');

    }
}

这是我的活动

<?php

namespace App\Events\EMailNotif;

use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use App\WFApprover;

class UserWorkflowEmail
{
    use Dispatchable, SerializesModels;

    public $wf_apprv;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(App\WFApprover $wf_apprv)
    {
        $this->wf_apprv = $wf_apprv;
    }

}

这是我的倾听者

<?php

namespace App\Listeners\EmailNotif;

use App\Events\EmailNotif\UserWorkflowEmail;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendWorkflowEmail
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserWorkflowEmail  $event
     * @return void
     */
    public function handle(UserWorkflowEmail $event)
    {
        dd($event);
    }
}

1 个答案:

答案 0 :(得分:0)

我在EventServiceProvider中使用它:

    protected $listen = [
        'App\Events\Signature\*' => [
            HandleSignature::class
        ],
    ];

原来我不得不:

    protected $listen = [
        InitiatedSuccessfully::class => [
            HandleSignature::class
        ],
        SignedSuccessfully::class => [
            HandleSignature::class
        ],
    ];

不知道为什么。我从Laravel 5.3升级到5.5并浪费了一个多小时。