如何将此存储函数转换为json

时间:2018-07-31 07:27:52

标签: php android laravel

我在laravel中使用了此功能,效果很好,但是我想插入一个json函数以在android上使用。

这是我的第一个可以帮助我或指导我取得好成绩的人。

public function storeavis(Request $request)
{

$avis = new AvisIntervention();
$avis ->qualité =$request->input('qualité');
$avis ->nbr_heure =$request->input('nbr_heure');
$avis ->service =$request->input('service');
$avis ->note =$request->input('note');
$avis ->client_id =$request->input('client_id');
$avis ->intervention_id = $request->input('intervention_id');
$avis->save();
return redirect('avis');

}

路线

Route::post('/storeavis', 'Avis_InterventionController@storeavis');

我总是收到以下消息:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: in file 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\Routing\ 
RouteCollection.php on line 255 Stack trace: 1. 
Symfony\Component\HttpKernel\Exception\ MethodNotAllowedHttpException->() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Routing\RouteCollection.php:255 2. Illuminate\Routing\RouteCollection- 
>methodNotAllowed() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Routing\RouteCollection.php:242 3. Illuminate\Routing\RouteCollection- 
>getRouteForMethods() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Routing\RouteCollection.php:176 4. Illuminate\Routing\RouteCollection- 
>match() C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Routing\Router.php:612 5. Illuminate\Routing\Router->findRoute() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Routing\Router.php:601 6. Illuminate\Routing\Router->dispatchToRoute() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Routing\Router.php:590 7. Illuminate\Routing\Router->dispatch() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Foundation\Http\Kernel.php:176 8. Illuminate\Foundation\Http\Kernel- 
>Illuminate\Foundation\Http{closure}() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Routing\Pipeline.php:30 9. Illuminate\Routing\Pipeline- 
>Illuminate\Routing{closure}() 
C:\xampp\htdocs\projet\vendor\fideloper\proxy\src\TrustProxies.php:56 10. 
Fideloper\Proxy\TrustProxies->handle() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Pipeline\Pipeline.php:149 11. Illuminate\Pipeline\Pipeline- 
>Illuminate\Pipeline{closure}() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Routing\Pipeline.php:53 12. Illuminate\Routing\Pipeline- 
>Illuminate\Routing{closure}() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Foundation\Http\Middleware\TransformsRequest.php:30 13. 
Illuminate\Foundation\Http\Middleware\TransformsRequest->handle()     
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Pipeline\Pipeline.php:149 14. Illuminate\Pipeline\Pipeline- 
>Illuminate\Pipeline{closure}() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Routing\Pipeline.php:53 15. Illuminate\Routing\Pipeline- 
>Illuminate\Routing{closure}() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Foundation\Http\Middleware\TransformsRequest.php:30 16. 
Illuminate\Foundation\Http\Middleware\TransformsRequest->handle() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Pipeline\Pipeline.php:149 17. Illuminate\Pipeline\Pipeline- 
>Illuminate\Pipeline{closure}() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Routing\Pipeline.php:53 18. Illuminate\Routing\Pipeline- 
>Illuminate\Routing{closure}() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Foundation\Http\Middleware\ValidatePostSize.php:27 19. 
Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Pipeline\Pipeline.php:149 20. Illuminate\Pipeline\Pipeline- 
>Illuminate\Pipeline{closure}() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Routing\Pipeline.php:53 21. Illuminate\Routing\Pipeline- 
>Illuminate\Routing{closure}() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Foundation\Http\Middleware\CheckForMaintenanceMode.php:46 22. 
Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Pipeline\Pipeline.php:149 23. Illuminate\Pipeline\Pipeline- 
>Illuminate\Pipeline{closure}() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Routing\Pipeline.php:53 24. Illuminate\Routing\Pipeline- 
>Illuminate\Routing{closure}() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Pipeline\Pipeline.php:102 25. Illuminate\Pipeline\Pipeline->then() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Foundation\Http\Kernel.php:151 26. Illuminate\Foundation\Http\Kernel- 
>sendRequestThroughRouter() 
C:\xampp\htdocs\projet\vendor\laravel\framework\src\Illuminate\ 
Foundation\Http\Kernel.php:116 27. Illuminate\Foundation\Http\   Kernel- 
>handle() C:\xampp\htdocs\projet\public\index.php:55

2 个答案:

答案 0 :(得分:1)

您可以像这样使用laravel的内置方法response()->json

public function store(Request $request)
{

    $avis = new AvisIntervention();
    $avis ->qualité =$request->input('qualité');
    $avis ->nbr_heure =$request->input('nbr_heure');
    $avis ->service =$request->input('service');
    $avis ->note =$request->input('note');
    $avis ->client_id =$request->input('client_id');
    $avis ->intervention_id = $request->input('intervention_id');
    $avis->save();

    return response()->json(['avis' => $avis]);

}

答案 1 :(得分:0)

  

在JSON中,值必须是以下数据类型之一:

a string
a number
an object (JSON object)
an array
a boolean
null
     

JSON值不能是以下数据类型之一:

a function
a date
undefined
class Cl
{
    public $val = 5;

    public function f(){
        echo '5';
    }

    public $func;

    public function __construct()
    {
        $this->func  = function () {
            return 'func';
        };
    }

}

$cl = new Cl();
$js = json_encode($js);
$cl = json_decode($js);

print_r($cl);
//            stdClass Object
//            (
//                [val] => 5
//                [func] => stdClass Object
//            (
//            )
//            
//        

You can store only 'values'