将多个对象传递到laravel

时间:2017-05-25 08:31:39

标签: laravel laravel-5

我有一个方法,其中有一些用户跟踪代码

public function settings(Request $request)
    {
      $id = Auth::id();
      $now = new DateTime();
      $page = 'Settings Page';
      $monitor = new Monitoring;
      $users_full_names = Auth::user()->users_full_names;
      $country = 'unseen';
      $device = $agent->device();
      $platform = $agent->platform();
      $browser = $agent->browser();
      $date = $now->format('Y-m-d H:i:s');
      $time = time();
      $ip = $request->ip();

      $data = compact('users_full_names','page','country','time','date','monitor','id', 'device', 'platform', 'browser', 'ip');

      Event::fire('simpleEvent', $data, $monitor);

      $uid = Auth::id();
      $settings = Settings::where('user_id','=',Auth::id())->first();
      return view('text.settings',compact('settings'));
    }

该方法使用此行将数据传递给事件

Event::fire('simpleEvent', $data, $monitor);

我已定义。这是我在web.php

中的事件代码
  Event::listen('simpleEvent', function($data,$monitor){
  $monitor->monitoring_user_id = Auth::id();
  $monitor->monitoring_user_names = $data->users_full_names;
  $monitor->monitoring_page_visited = $data->page;
  $monitor->monitoring_ip = $data->ip;
  $monitor->monitoring_date_of_visit = $data->date;
  $monitor->monitoring_time_of_visit = $data->time;
  $monitor->monitoring_country = $data->country;
  $monitor->monitoring_date = $data->date;
  $monitor->monitoring_device = $data->platform;
  $monitor->save();
  });

当我运行代码时,我收到此错误

  

at HandleExceptions-> handleError(2,'尝试分配属性   non-object' /#39; /var/www/html/letextify/routes/web.php' ;, 14,   数组('数据' =>' Radagast','监控' =>'成功登录')in in   web.php第14行

这是第14

$monitor->monitoring_user_id = Auth::id();

这是dd($data)输出

array:10 [▼
  "users_full_names" => "Radagast"
  "page" => "Successfully Logged In"
  "country" => "unseen"
  "time" => 1495705235
  "date" => "2017-05-25 09:40:35"
  "monitor" => Monitoring {#331 ▼
    #table: "monitoring"
    #connection: null
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: false
    +wasRecentlyCreated: false
    #attributes: []
    #original: []
    #casts: []
    #dates: []
    #dateFormat: null
    #appends: []
    #events: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #fillable: []
    #guarded: array:1 [▶]
  }
  "device" => false
  "platform" => "Ubuntu"
  "browser" => "Firefox"
  "ip" => "100.208.40.100"
]

如何纠正错误?。

2 个答案:

答案 0 :(得分:0)

将ID添加到$ data Array

在紧凑数组中添加'id'。以便它可以在$ data-> id

中使用
$data = compact('id', 'users_full_names','page','country','time','date','monitor','id', 'device', 'platform', 'browser', 'ip');

$ monitor已经在$ data中可用。所以你可以改变你的代码如下:

更改消防代码

Event::fire('simpleEvent', $data);

更改监听代码

也无需再次调用Auth :: id(),因为它在$ data-> id中可用,请检查以下代码:

Event::listen('simpleEvent', function($data){

  $monitor = $data->monitor;                            //check this line

  $monitor->monitoring_user_id = $data->id;             //check this line

  $monitor->monitoring_user_names = $data->users_full_names;
  $monitor->monitoring_page_visited = $data->page;
  $monitor->monitoring_ip = $data->ip;
  $monitor->monitoring_date_of_visit = $data->date;
  $monitor->monitoring_time_of_visit = $data->time;
  $monitor->monitoring_country = $data->country;
  $monitor->monitoring_date = $data->date;
  $monitor->monitoring_device = $data->platform;
  $monitor->save();
  });

答案 1 :(得分:0)

我这样解决了

Event::listen('simpleEvent', function($data){
  $monitor = $data['monitor'];
  $monitor->monitoring_user_id = $data['id'];
  $monitor->monitoring_user_names = $data['users_full_names'];
  $monitor->monitoring_page_visited = $data['page'];
  $monitor->monitoring_ip = $data['ip'];
  $monitor->monitoring_date_of_visit = $data['date'];
  $monitor->monitoring_time_of_visit = $data['time'];
  $monitor->monitoring_country = $data['country'];
  $monitor->monitoring_date = $data['date'];
  $monitor->monitoring_device = $data['platform'];
  $monitor->save();
  });