繁体   English   中英

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

[英]Laravel 5.4: Cannot retrieve data from a relationship

我试图通过将每个名称存储在两个UNION'd表(访问和报告)的数组中来显示每个故障单的受理人的姓名(来自Users表的外键),但它给了我这个错误。 ErrorException未定义属性: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();
    });
   }

它给了我这个错误

结果应该是这样的

你应该使用集合的merge方法:

$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);

您的问题并不完全清楚,但似乎您实际上只需要用户名。 这是未经测试的,因为我没有要测试的数据集。 但它应该找到具有指定的访问权限或已分配状态的所有用户的名称。

$names = DB::table('users')
    ->select('users.name')
    ->leftJoin('accesses', function ($join) {
        return $join->on('users.id', '=', 'accesses.assigned_to')
            ->where('accesses.state', '=', 'Assigned');
    })
    ->leftJoin('reports', function ($join) {
        return $join->on('users.id', '=', 'reports.assigned_to')
            ->where('reports.state', '=', 'Assigned');
    })
    ->where(function ($query) {
        return $query->whereNotNull('accesses.id')
            ->orWhereNotNull('reports.id');
    })
    ->groupBy('users.id');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM