[英]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.