iMatti
iMatti

Reputation: 13

Laravel 5.8 Internal server error with ajax

i am getting error, Internal server error,

GET http://isp.local/teachers/168/edit 500 (Internal Server Error)

Controller:

public function edit($id)
  {
  $teacher = DB::table('teachers')
    ->find($id)
    ->select('*')
    ->first();
return response()->json([
          'status' => 'success',
          'teacher' => $teacher,
      ]);
  }

while when i make following changes in controller i am getting correct results, what is issue in above code?

Controller:

 public function edit($id)
      {


$teacher = Teacher::find($id);

    return response()->json([
              'status' => 'success',
              'teacher' => $teacher,
          ]);
      }

Upvotes: 0

Views: 64

Answers (2)

Tim Lewis
Tim Lewis

Reputation: 29278

That query is wrong. ->find() executes the query, as does ->first(), and everything is selected by default, so ->select('*') is unnecessary.

$teacher = DB::table('teachers')->find($id);

Should be enough. But you're already using the correct

$teacher = Teacher::find($id);

So there isn't much point to using the DB::table() approach.

Upvotes: 2

Khachik
Khachik

Reputation: 9

Try

 ->whereId($id)

or

where('id',$id)

Upvotes: -1

Related Questions