简体   繁体   English

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

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

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

It gives me this error 它给了我这个错误

The results should be like this 结果应该是这样的

You should use merge method of collection: 你应该使用集合的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);

Your question isn't entirely clear, but it seems that you only actually need the name of the user. 您的问题并不完全清楚,但似乎您实际上只需要用户名。 This is untested, because I don't have a data set to test against. 这是未经测试的,因为我没有要测试的数据集。 But it should find the name of all users who have an assigned Access or an assigned State. 但它应该找到具有指定的访问权限或已分配状态的所有用户的名称。

$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