Skip to content
Advertisement

why am i getting null value from my database in laravel when trying to find specific user?

Am trying to display information of a specific student in my database using student ID. I seem to get null value yet the student exist in the database. i seriously do not know why am getting this null value.

this is what i did

public function showResult(Request $request, $std_id)
    {   

      $user = Student::find($std_id);
      if (empty($user)) {
        return redirect('/user')
      }

        $academic = $request->input('academic_year');
        $std = $request -> input('studentID');

       $results = DB::table('students')
                  ->select('*')
                  ->join('results', 'students.std_id', 'results.student_results_id')
                  ->where('student_results_id', '=', $std)
                  ->where('academic_year', '=', $academic)
                  ->get();


                  return view('resultsdisplay',[
                  'results' => $results,
                  ]);
        }
}

This is my Route:

Route::get('/student/resultshow/{std_id}', 'ResultsController@showResult');

My studentprofile.blade.php

<li class="list-group-item">
            <form action="{{URL::to('/student/resultshow/{!! $studentprofilePage->std_id !!}')}}" method="GET" enctype="multipart/form-data">

             <input type="text" name="studentID" value="{!! $studentprofilePage->std_id !!}" hidden="">

            <input type="text" name="academic_year" placeholder="Academic year.."> 
            <button>view</button>    
            </form>
            </li>

Advertisement

Answer

A couple of things to test. First, check the $std_id variable as it comes into the method to make sure this is translating correctly from your form / route into the method.

public function showResult(Request $request, $std_id){
     dd($std_id);

If this is a properly formed integer/id, check the database just to make sure that id is actually assigned within the students table, and it is not soft-deleted if you are using soft-deletes (deleted_at column).

If all good there, take a look at the Student model itself. Is the model pointing to the right table? Laravel assumes the table is named ‘students’. If not, have you declared the table within the model?

public $table = 'mySpecialStudentsTableName';

Also, is the id type correct against what is coming in. IE I assume it is an integer, but if it is looking for a string / uuid, is that properly coming out of that $std_id variable?

Finally – your form is not sending the correct id:

<form action="{{URL::to('/student/resultshow/{!! $studentprofilePage->std_id !!}')}}"

Should be:

<form action="{{URL::to('/student/resultshow/'. $studentprofilePage->std_id)}}"
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement