Hi everyone,
so, I have several tables (I'll try to simplify):
- reports (report_id, user_id, event_id, report_content)
- hospitals (hospital_id, hospital_name)
- events (event_id, user_id, hospital_id, event_date)
- users (user_id, first_name, last_name)
The idea is that a user can create an event, and after the event is done, it will write a report on how the event went.
The report has to show the date of the event and in which hospital the event took place.
So far, I managed to get everything else within my model, but I'm having trouble retrieving the hospital name.
Here is the report model:
class Report extends Model
{
use HasFactory;
protected $fillable = [
'report_id',
'user_id',
'event_id',
'report_content',
'report_status',
'created_by',
'created_at',
'updated_at'
];
protected $primaryKey = 'report_id';
protected $table = 'reports';
public function reportVolunteerInfo() {
return $this->belongsTo('App\Models\User', 'user_id', 'user_id');
}
public function reportCreatedByInfo() {
return $this->belongsTo('App\Models\User', 'created_by', 'user_id');
}
public function reportHospitalInfo() {
return $this->belongsTo('App\Models\Hospital', 'hospital_id', 'event_id');
}
public function reportEventDate() {
return $this->belongsTo('App\Models\Event', 'event_id', 'event_id');
}
}
And if I try this in my blade template - it returns "Hospital name"
{{$report->reportHospitalInfo->hospital_id ?? 'Hospital name'}}
Now, I am aware that hospital_id and event_id are not a match, but I'm not sure what would be wrong here since reportEventDate() also doesn't work.
Here are also models for Event and Hospital:
Event:
class Event extends Model
{
use HasFactory;
protected $fillable = [
'event_id',
'user_id',
'hospital_id',
'report_id',
'event_date',
'event_content',
'created_by'
];
protected $primaryKey = 'event_id';
protected $table = 'events'; //definiranje koju tablicu gledamo
}
Hospital:
class Hospital extends Model
{
use HasFactory;
protected $fillable = [
'hospital_id',
'full_hospital_name',
'short_hospital_name',
'hospital_color',
'hospital_location',
'hospital_coordinator',
'hospital_max_people',
'hospital_status'
];
protected $primaryKey = 'hospital_id';
protected $table = 'hospitals';
}