I am having a problem with my relationships in model using composite key. I am using the package ThiagoprzCompositeKeyHasCompositeKey. If i am only using a single key for the relationships in models, I am not having a problem. I also wanna ask too if what is the correct usage for the controllers. Here is my current controller
Here is so far what I got from debugging From PHP Tinker
public function index(Request $request)
{
try {
$crse_id = $request->input('crse_id');
$crse_offer_nbr = $request->input('crse_offer_nbr');
$strm = $request->input('strm');
$session_code = $request->input('session_code');
$class_section = $request->input('class_section');
$classData = ClassTbl::with([
'classChrstc',
'institution:institution,descr',
'termTable:strm,descr',
'academicSubject:subject'
])
->where('crse_id', $crse_id)
->where('crse_offer_nbr', $crse_offer_nbr)
->where('strm', $strm)
->where('session_code', $session_code)
->where('class_section', $class_section)
->first();
if (!$classData) {
return response()->json([
'success' => false,
'message' => 'Class data not found.'
], 404);
}
return response()->json([
'success' => true,
'data' => $classData,
'message' => 'Class data retrieved successfully.'
]);
} catch (Exception $e) {
return response()->json([
'success' => false,
'message' => 'Failed to retrieve class data.',
'error' => $e->getMessage()
], 500);
}
}
public function classTbl()
{
return $this->belongsTo(ClassTbl::class, 'crse_id', 'crse_id')
->whereColumn('class_tbl.crse_offer_nbr', 'class_chrstcs.crse_offer_nbr')
->whereColumn('class_tbl.strm', 'class_chrstcs.strm')
->whereColumn('class_tbl.session_code', 'class_chrstcs.session_code')
->whereColumn('class_tbl.class_section', 'class_chrstcs.class_section');
}
public function classChrstc()
{
return $this->hasMany(ClassChrstc::class, 'crse_id', 'crse_id')
->whereColumn('class_chrstcs.crse_offer_nbr', 'class_tbl.crse_offer_nbr')
->whereColumn('class_chrstcs.strm', 'class_tbl.strm')
->whereColumn('class_chrstcs.session_code', 'class_tbl.session_code')
->whereColumn('class_chrstcs.class_section', 'class_tbl.class_section');
}