2017-07-31 60 views
3

我試圖通過將每個名稱存儲在來自兩個UNION表的數組中來顯示每個票據的受讓人(用戶表的外鍵)的名稱(訪問和報告)但它給了我這個錯誤。 ErrorException 未定義的屬性:stdClass :: $受理人。Laravel 5.4:無法從關係中檢索數據

//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

+0

您正在使用'DB'外觀,而不是在模型上查詢。所以你不會有任何關係。 – fubar

+0

你只是想要受派人的名字?如果是這樣,爲什麼要查詢所有的字段?如果沒有,請在您的問題中添加更多信息。 – fubar

回答

0

,您應該使用收集的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); 
+0

但如果我使用口才,聯盟將無法工作,因爲表沒有相同數量的列。 –

+0

更新了我的答案,它可能會對你有所幫助。 @GrantGubatan –

+0

@GrantGubatan這不起作用。雄辯的集合根據模型主鍵合併結果。如果兩個結果集中都存在相同的主鍵,則它們將被覆蓋。 – fubar

0

你的問題是不完全清楚,但似乎只有你實際需要的名字的用戶。 這是未經測試的,因爲我沒有要測試的數據集。但它應該找到具有分配的訪問權限或分配的狀態的所有用戶的名稱。

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