我想更新 Room 表中的ResidentID。其中 ResidentID 是来自Group Table的外键引用。当我手动更新 ResidentId =2 时它运行良好,但我想将 Group id 从 $allot_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');
}
}