gpt4 book ai didi

mysql - laravel REST api post 方法

转载 作者:行者123 更新时间:2023-11-29 12:45:51 24 4
gpt4 key购买 nike

我在 laravel 中制作了一个 api,但是当我从 post 请求发送时,它不显示任何内容,只有当我发送 url 中的值时,它才起作用,我做错了,这里是我的代码!

$user = new userInscription;

$user->nom = Request::get('name');
$user->pseudo = Request::get('pseudo');
$user->userId = Request::get('userId');
$user->hasFiat = Request::get('hasFiat');
$user->optin = Request::get('optin');
$user->mail = Request::get('mail');

$pseudo = Input::get('pseudo');
$userId = Input::get('userId');
$hasFiat = Input::get('hasFiat');

if($pseudo == '' || $hasFiat == '' )
{
return Response::json( array(
'status' => 'ko',
'message' => 'missing mandatory parameters')
);
}

else if($userId == '')
{
if( $user->save() )
{
$id = DB::table('user')
->where('pseudo','LIKE',$pseudo)
->pluck('userId');

return Response::json(array(
'status' => 'ok',
'message' => 'success',
'userId' => $id
));
}
else
{
return Response::json(array(
'message' => 'error while saving this user !!',
));
}
}

最佳答案

Laravel REST-ful(资源丰富) Controller 具有预先配置的路由(可以重新配置):

根据:http://laravel.com/docs/controllers#resource-controllers

+-----------+---------------------------+---------+------------------+
| Verb | Path | Action | Route Name |
+-----------+---------------------------+---------+------------------+
| GET | /resource | index | resource.index |
| GET | /resource/create | create | resource.create |
| POST | /resource | store | resource.store |
| GET | /resource/{resource} | show | resource.show |
| GET | /resource/{resource}/edit | edit | resource.edit |
| PUT/PATCH | /resource/{resource} | update | resource.update |
| DELETE | /resource/{resource} | destroy | resource.destroy |
+-----------+---------------------------+---------+------------------+

引用表格,每个Verb必须对应于 Controller 中的action方法。

例如,如果您的资源路线注册为:

Route::resource('user', 'userInscriptionController');

然后发布到user资源,您需要有userInscriptionController@store操作(即在 store() 中调用 userInscriptionController 的方法。

为了避免手动创建这些操作,您可以使用 Laravel 的 artisan controller:make

php artisan controller:make userInscriptionController

它将为您生成所有这些操作,然后您只需填写逻辑即可完成资源。

关于mysql - laravel REST api post 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25443549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com