I have a table and its respective Qualification model, where I have 8 notes, average, exam, final grade. In my controller I calculate the average with the notes other than 0, and the final grade.
function guardarCalificacion(CalificacionRequest $request) {
$calificacion = new Calificacion($request->all());
$calificacion->id_profesor = auth('profesor')->user()->id;
$profesor = Profesor::find(auth('profesor')->user()->id);
$mis_asignaturas = $profesor->asignaturas->all();
$cantidad = 0;
if (($calificacion->n1) != null) {
$cantidad = $cantidad + 1;
} else {
$calificacion->n1 = 0;
}
if ($calificacion->n2 != null) {
$cantidad = $cantidad + 1;
} else {
$calificacion->n2 = 0;
}
if ($calificacion->n3 != null) {
$cantidad = $cantidad + 1;
} else {
$calificacion->n3 = 0;
}
if ($calificacion->n4 != null) {
$cantidad = $cantidad + 1;
} else {
$calificacion->n4 = 0;
}
if ($calificacion->n5 != null) {
$cantidad = $cantidad + 1;
} else {
$calificacion->n5 = 0;
}
if ($calificacion->n6 != null) {
$cantidad = $cantidad + 1;
} else {
$calificacion->n6 = 0;
}
if ($calificacion->n7 != null) {
$cantidad = $cantidad + 1;
} else {
$calificacion->n7 = 0;
}
if ($calificacion->n8 != null) {
$cantidad = $cantidad + 1;
} else {
$calificacion->n8 = 0;
}
$calificacion->promedio = ($calificacion->n1 + $calificacion->n2 + $calificacion->n3 + $calificacion->n4 + $calificacion->n5 + $calificacion->n6 + $calificacion->n7 + $calificacion->n8) / $cantidad;
if ($calificacion->examen != null) {
$calificacion->final = ($calificacion->promedio + $calificacion->examen) / 2;
}
$calificacion->save();
flash('Calificacion agregada exitosamente!', 'success');
return view('datos-profesor/asignaturas')->with('calificacion', $calificacion)->with('profesor', $profesor)->with('mis_asignaturas', $mis_asignaturas);
}
What I want is to get that variable of average and final note of my table, so that they do not depend on each other. that those two variables are calculated in the controller and displayed in the view, without the need for my Qualifications table to have those two attributes ($calificacion->promedio , $calificacion->final)
, how can I do that and send them to my view? I have the idea of simply placing
$promedio = ($calificacion->n1 + $calificacion->n2 + $calificacion->n3 + $calificacion->n4 + $calificacion->n5 + $calificacion->n6 + $calificacion->n7 + $calificacion->n8) / $cantidad;
$final = ($calificacion->promedio + $calificacion->examen) / 2;
But I do not know how to send it to view, this is my view:
<table class="table table-bordered">
<tr>
<th>N1</th>
<th>N2</th>
<th>N3</th>
<th>N4</th>
<th>N5</th>
<th>N6</th>
<th>N7</th>
<th>N8</th>
<th>Promedio </th>
<th>Examen </th>
<th>Final </th>
<th>Observacion </th>
<th>Opciones </th>
</tr>
@foreach($mis_notas as $mis_notas)
<tr>
<td>{{$mis_notas->n1}}</td>
<td>{{$mis_notas->n2}}</td>
<td>{{$mis_notas->n3}}</td>
<td>{{$mis_notas->n4}}</td>
<td>{{$mis_notas->n5}}</td>
<td>{{$mis_notas->n6}}</td>
<td>{{$mis_notas->n7}}</td>
<td>{{$mis_notas->n8}}</td>
<td>{{$mis_notas->promedio}}</td>
<td>{{$mis_notas->examen}}</td>
<td>{{$mis_notas->final}}</td>
<td>{{$mis_notas->observacion}}</td>
<td><a class="btn btn-warning" href="{{URL('modificar/calificacion', array($mis_notas->id_alumno, $mis_notas->id_asignatura, $mis_notas->id ))}}"> Agregar Notas</a></td>
</tr>
@endforeach
</table>
How could I do it? to send variable $promedio
and $final
to the view? since it is within% co_of% of my_notes ??
Qualification Model
protected $table = "calificaciones";
protected $fillable = ['n1', 'n2', 'n3', 'n4', 'n5', 'n6', 'n7', 'n8', 'promedio', 'examen', 'final', 'observacion', 'id_alumno', 'id_asignatura', 'id_profesor'];
public function alumno()
{
return $this->belongsTo(Alumno::class, 'id_alumno', 'id');
}
public function asignatura()
{
return $this->belongsTo(Asignatura::class, 'id_asignatura', 'id');
}
public function profesor()
{
return $this->belongsTo(Profesor::class, 'id_profesor', 'id');
}
Student Model
protected $table = "alumnos";
protected $fillable = ['rut', 'nombre', 'apellido_paterno', 'apellido_materno', 'email', 'password', 'sexo', 'telefono', 'foto', 'fecha_nacimiento', 'direccion', 'id_curso', 'id_apoderado'];
protected $hidden = ['password', 'remember_token'];
public function curso()
{
return $this->belongsTo(Curso::class,'id_curso','id');
}
public function apoderado()
{
return $this->belongsTo(Apoderado::class,'id_apoderado','id');
}
public function matriculas()
{
return $this->hasMany('App\Matricula');
}
public function asignaturas()
{
return $this->belongsToMany('App\Asignatura');
}
public function conductas()
{
return $this->hasMany('App\Conducta','id_alumno');
}
public function calificaciones()
{
return $this->hasMany('App\Calificacion','id_alumno');
}
Subject Model
protected $table = "asignaturas";
protected $fillable = ['nombre', 'horario', 'periodo', 'codigo', 'id_sala', 'id_curso', 'id_profesor'];
public function sala()
{
return $this->belongsTo(Sala::class,'id_sala','id');
}
public function curso()
{
return $this->belongsTo(Curso::class,'id_curso','id');
}
public function profesor()
{
return $this->belongsTo(Profesor::class,'id_profesor','id');
}
public function alumnos()
{
return $this->belongsToMany('App\Alumno')->withTimestamps();
}
public function conductas()
{
return $this->hasMany('App\Conducta','id_asignatura');
}
public function eventos()
{
return $this->hasMany('App\Evento','id_asignatura');
}
public function calificaciones()
{
return $this->hasMany('App\Calificacion','id_asignatura');
}
The student enrolls in a course and the course has subjects