Php 如何获取从两个表收集数据并更新列的对象的属性?

Php 如何获取从两个表收集数据并更新列的对象的属性?,php,laravel,Php,Laravel,我想更新房间表中的ResidentID。其中ResidentID是组表的外键引用。当我手动更新ResidentId=2时,它工作得很好,但我希望将组id从$allot\u apps对象发送到ResidentId public function View_GroupStudent() { $caps = Rooms::selectRaw('count(id) as c')->count(); $allot_apps = AllotmentApps::join('Group

我想更新房间表中的ResidentID。其中ResidentID是组表的外键引用。当我手动更新ResidentId=2时,它工作得很好,但我希望将组id从$allot\u apps对象发送到ResidentId

public function View_GroupStudent()
{  
    $caps = Rooms::selectRaw('count(id) as c')->count();
    $allot_apps = AllotmentApps::join('Groups','Groups.id','AllotmentApps.grpID')- 
    >orderBy('Groups.Group_CGPA', 'Desc')->get()->groupBy(
      function($item) { 
        return $item->grpID;
    }
  )->take($caps);
  $merits=Rooms::all()->toArray();
  foreach ($merits as $key) {
 $merits=Rooms::where('id',$key)->Update(['ResidentID'=>'2']);
  }
    return view('admin.ViewGroups', compact('allot_apps'));
}

分配应用程序模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AllotmentApps extends Model
{
//

protected $fillable = [
    'sname', 'fname', 'Roll_No','cnic','domicile','cgpa','Department','Session','degreeProgram','contactNo','GcontactNo','preAddress','permAddress','fee','challanNo','app_status','fee_status','grpID',
];


protected $primaryKey = 'id';
protected $table = 'AllotmentApps';

 public function groups()
{
    return $this->belongsTo('App\Groups');
}
  }
 <?php

  namespace App;

 use Illuminate\Database\Eloquent\Model;

  class Groups extends Model
{
//
protected $fillable = ['Group_CGPA',];

protected $primaryKey = 'id';

public function allotmentApps()
{
    return $this->hasMany('App\AllotmentApps');
}
   }
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Rooms extends Model


protected $fillable = ['id','HostelID','ResidentID'];

protected $primaryKey = 'id';
 {
   public function hostels()
{
   
   return $this->belongsTo('App\Hostels');
}
  }

向我们展示
var\u dump($mertis)和var\u dump($allot\u apps)
我已经编辑了问题并添加了转储图片请看一看,在这些图片上,您能给我们展示表格的模型吗?作为一个侧节点,您正在覆盖foreach循环中的mertives变量。我已经在这个问题中添加了模型,请查看一下,并让我知道我的错误在哪里。我怎样才能实现我的最终目标?