【问题标题】:Postman return null data even if data exist [closed]即使数据存在,邮递员也会返回空数据[关闭]
【发布时间】:2021-05-26 11:06:22
【问题描述】:

我不知道为什么即使数据存在它也不起作用。

查询

$employee = Employee::where('email', request('email'))->first();

if(!$employee)
{ 
   return response()->json(['error' => 'User not found.'], 401);
}

API.php

Route::post('employeelogin', 'EmployeeController@employeeLogin');

邮递员"error": "User not found."出错

【问题讨论】:

  • postman 中如何获取数据?
  • 你的request('email')有错误应该是$request->email
  • @KamleshPaul imgur.com/ghRBJpl
  • 我猜是邮递员从x-www-urlencoded标签发送的
  • @KamleshPaul 如果我在 Postman 中以JSON 格式发送数据,它会起作用

标签: php laravel api postman query-builder


【解决方案1】:

如果我在 Postman 中以 JSON 格式发送数据,它会起作用

【讨论】:

    【解决方案2】:

    使用 postman 测试 api 时 首先在标题部分添加这个

    X-Requested-With = XMLHttpRequest

    然后使用 x-form-data 部分,而不是 form-data 部分

    【讨论】:

      【解决方案3】:
      You should do like this
      
      // controller function
      public function function($email)
      {
       $employee = Employee::where('email', $email)->first();
      }
      
      // route should be get not post
      Route::get('employeelogin/{email}', 'EmployeeController@employeeLogin');
      
      // postman use get method
      http://127.0.0.1:8000/employeelogin/test@gmail.com
      
      it will work!
      

      【讨论】:

      • 但是我必须同时提供电子邮件和密码,而不仅仅是电子邮件,然后如果电子邮件和密码正确,它会返回一个成功令牌。
      • 我也在为我的另一个表做同样的事情,它使用POST 方法。
      • 你是在使用 laravel 护照进行认证吗?
      • 是的,我正在使用 laravel 护照进行身份验证,我为员工表创建了一个不同的 guard
      猜你喜欢
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多