php - 拉维尔 5.4 : Cannot retrieve data from a relationship

标签 php mysql database laravel relationships

我试图通过将每个名称存储在两个 UNION 表(Accesses 和 Reports)的数组中来显示每张票证的受让人名称(来自 Users 表的外键),但它给了我这个错误。错误异常 未定义的属性: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

最佳答案

您应该使用 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);

关于php - 拉维尔 5.4 : Cannot retrieve data from a relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45406179/

相关文章:

mysql - 如何使用 laravel 将表添加到我预先存在的数据库中?

php - 从共享内存中检索时,PHP 是否复制变量?

php - 找不到 httpd.conf PHP 模块

mysql - "Table doesn' t存在”问题

MySQL 错误 "' WITH' 在此位置对于需要 ALTER、ANALYZE 的服务器版本无效...”

更新字符串中三个字符的 SQL 语句

javascript - 在 sweetalert 上显示文件的上传百分比(一个变量)

javascript - 我如何添加日期格式以从下午 4 点到 9 点 :45am 禁用按钮

mysql - 选择父项的所有子项

c# - 查找包含特定字母/符号的字符串的特定部分,然后创建对这些部分的引用