samedi 16 mars 2019

Save data generated to a specific table in database

This is the output in my view

This is the code in my controller

public function showFinalYear(){

    $finalyear_students = Matrix::get()->where('total_subject_left','<',10);

    return view('admin.final_year_list')->with(compact('finalyear_students'));}

This is the code in my model

class Matrix extends Model
{
  protected $table = 'matrices';
  public $timestamps = false;

  public function getSubjects()
  {
    $subjects = [];
    $subjects[] = $this->S1 == 1 ? 'S1' : null;
    $subjects[] = $this->S2 == 1 ? 'S2' : null;
    $subjects[] = $this->S3 == 1 ? 'S3' : null;
    $subjects[] = $this->S4 == 1 ? 'S4' : null;
    $subjects[] = $this->S5 == 1 ? 'S5' : null;
    $subjects[] = $this->S6 == 1 ? 'S6' : null;
    .... and so on

    $subjects = array_filter($subjects);

    return implode(',', $subjects);
   }
 }

This is my code in the view

<tbody>
  @foreach ($finalyear_students as $fy)
    <tr class="gradeX">
      <td></td>
      <td></td>
    </tr>
  @endforeach
</tbody>

the problem happened when I want to save the student name and subjects based on the output where the code save the student name and subject doesn't work.

This is what I have tried but it does not work.

public function showFinalYear(){

    $finalyear_students = Matrix::get()->where('total_subject_left','<',10);

    DB::table('finalyear_students')
        ->updateOrInsert(
            ['student_name'=>$finalyear_students->student_name],
            ['subject'=>$finalyear_students->getSubjects()]
        );

    return view('admin.final_year_list')->with(compact('finalyear_students'));
}

Hopefully, someone can help me to solve this, Thanks in advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire