dongtang7347 2017-07-31 00:57
浏览 56

Laravel 5.4:无法从关系中检索数据

I'm trying to display the name of the assignee (foreign key from Users table) of each ticket by storing each name in an array from two UNION'd tables (Accesses and Reports) but it gives me this error. ErrorException Undefined property: stdClass::$assignee.

//HomeController
    $accesses = DB::table('accesses')
                ->select(array('id', 'fullname','emp_id','shift','state','resolved_at', 'closed_at','assigned_to'))
                ->where('state','=','Assigned');

    $all = DB::table('reports')
                ->select(array('id', 'fullname','emp_id','shift','state','resolved_at', 'closed_at','assigned_to'))
                ->union($accesses)
                ->where('state', '=', 'Assigned')
                ->get();

    $names[] = array();

    foreach ($all as $one)//store in array to display in a chart
    {
      $names[] = $one->assignee->name; //error here
    }




   //Report Model
   public function assignee()
   {
      return $this->belongsTo(User::class, 'assigned_to');
   }

   //Access Model
   public function assignee()
   {
      return $this->belongsTo(User::class, 'assigned_to');
   }

  //Report Migration
  public function up()
  {
    Schema::create('reports', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->nullable();
        $table->string('fullname');
        $table->string('emp_id');
        $table->string('shift');
        $table->longText('report');
        $table->string('status')->default('Pending'); //Pending, Approved
        $table->string('state')->default('Open'); //Open, Assigned, Resolved, Closed
        $table->date('resolved_at')->nullable();
        $table->date('closed_at')->nullable();
        $table->integer('assigned_to')->nullable();
        $table->longText('action')->nullable();
        $table->timestamps();
    });
   }

   //Access Migration
   public function up()
   {
    Schema::create('accesses', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->nullable();
        $table->string('fullname');
        $table->string('emp_id');
        $table->string('shift');
        $table->string('request');
        $table->longText('note')->nullable();
        $table->string('status')->default('Pending'); //Pending, Approved
        $table->string('state')->default('Open'); //Open, Assigned, Resolved, Closed
        $table->date('resolved_at')->nullable();
        $table->date('closed_at')->nullable();
        $table->integer('assigned_to')->nullable();
        $table->longText('action')->nullable();
        $table->timestamps();
    });
   }

It gives me this error

The results should be like this

  • 写回答

2条回答 默认 最新

  • dpqvwgr74759 2017-07-31 01:34
    关注

    You should use merge method of collection:

    $accesses = Access::select(array('id', 'fullname','emp_id','shift','state','resolved_at', 'closed_at','assigned_to'))
                ->where('state','=','Assigned')
                ->get();
    
    $reports = Report::select(array('id', 'fullname','emp_id','shift','state','resolved_at', 'closed_at','assigned_to'))
                ->where('state', '=', 'Assigned')
                ->get();
    
    $all = $accesses->merge($reports);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊