【发布时间】:2019-12-18 10:22:25
【问题描述】:
所以我在 laravel 中为客户制作了一个 crud API 并想在 postman 中对其进行测试,但我有一个问题/疑问。 我试图在 api 中找到客户端的 id,但找不到,我不知道为什么。 我想在邮递员 url 中给出 id。
在 api 路由中,我有这样的 $id:
Route::match(['get', 'post'], "/clients/{id?}", "ApiV2Controller@clients");
在clients函数中:
public function clients(Request $request, $id = null){
if($id){
$client = Client::find($id);
if(is_null($client)){
return $this->sendError("404", 'client not found');
}
}
else{
$client = new Client;
}
$client->clt_name = $request->name;
$client->clt_adress = $request->adress;
$client->save();
}
邮递员网址:
localhost:8000/api/v2/clients/19?name=TestName&adress=TestAdress
上面写着client not found。
提前致谢!
【问题讨论】:
-
你能把整个客户端函数粘贴到这里吗?你的数据库里有没有 id 19 的客户端?
-
编辑了帖子,没有id = 19的客户
-
如果没有 id = 19 的客户端,则输出正确。那有什么问题呢?
-
它应该创造一个新的记录,但它没有。不确定我是否设置了正确的邮递员网址。
-
不,您没有考虑 id 不为 null 并且该 id 未存储在您的数据库中的情况。