【问题标题】:Laravel 5.8 Internal server error with ajaxLaravel 5.8 ajax 内部服务器错误
【发布时间】:2019-10-29 03:31:01
【问题描述】:

我收到错误,内部服务器错误,

GET http://isp.local/teachers/168/edit500(内部服务器错误)

控制器:

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

当我在控制器中进行以下更改时,我得到了正确的结果,上面的代码有什么问题?

控制器:

 public function edit($id)
      {


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

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

【问题讨论】:

标签: laravel laravel-5.8


【解决方案1】:

那个查询是错误的。 ->find() 执行查询,->first() 也是如此,并且默认选择所有内容,因此不需要->select('*')

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

应该够了。但是你已经使用了正确的

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

所以使用DB::table() 方法没有多大意义。

【讨论】:

    【解决方案2】:

    试试

     ->whereId($id)
    

    where('id',$id)
    

    【讨论】:

    • 同时启用调试器以查看来自 env 文件 APP_DEBUG=true 的正确错误
    • 谢谢,你能告诉我这里的服务器有什么问题吗?
    • Find 检索给定表的结果,因此您不能使用 get,first,find 之后的所有方法。
    • 从浏览器网络端你会看到响应
    • 它返回空白。
    猜你喜欢
    • 2019-08-18
    • 2017-04-12
    • 2020-02-12
    • 2018-04-16
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    相关资源
    最近更新 更多