how to convert this store function to json

Published 2 months ago by chagouani

Strore function

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 redirect('avis');

  }
lostdreamer_nl

If you mean: How can I make it so that this endpoint can be used by AJAX, I would return a JSON representation of the new object instead of a redirect:

public function store(Request $request)
    {
    // You should really validate your data as well...... Dont ever trust a user.
    // https://laravel.com/docs/5.6/validation
    $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->toArray());
  }
chagouani

@lostdreamer_nl sorry i have problém whith my application so i have solved now and i have make you function i have this error

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint 
    violation
lostdreamer_nl

Your AvisIntervention database table has a few fields that are not nullable, have no default values and now you're getting constraint violations.

I'd check if client_id and intervention_id are actually set in the request.

cmdobueno

public function store(Request $request)
{
    
        $avis = new AvisIntervention();
        $avis ->qualité =$request->qualité ?? "Default Value";
        $avis ->nbr_heure =$request->nbr_heure ?? "Default Value";
        $avis ->service =$request->service ?? "Default Value";
        $avis ->note =$request->note ?? "Default Value";
        $avis ->client_id =$request->client_id ?? "Default Value";
        $avis ->intervention_id = $request->intervention_id ?? "Default Value";
        $avis->save();

    if( $request->ajax() ){
        return response()->json($avis->toArray());
    } else {
        return redirect('avis');
    }
        
  }


Here is a small snippet that should help, it gives you the ability to control what happens with both ajax and non-ajax (if required).

Additionally you should have validation to make sure the data is actually being sent..

https://laravel.com/docs/5.6/validation#validation-quickstart

Since you have no validation, I added the ?? operator to check that each field is set, you could then change "Default Value" to whatever. This would NOT be required if you had VALIDATION.

chagouani

@cmdobueno when i have make your code i have this error :

Could not get any response
There was an error connecting to http://192.168.1.233/projet/public/api/avs.
Why this might have happened:
The server couldn't send a response:
Ensure that the backend is working properly
Self-signed SSL certificates are being blocked:
Fix this by turning off 'SSL certificate verification' in Settings > General
Proxy configured incorrectly
Ensure that proxy is configured correctly in Settings > Proxy
Request timeout:
Change request timeout in Settings > General
chagouani

@lostdreamer_nl this is my table migration

 public function up()
{
    Schema::create('avis_interventions', function (Blueprint $table) {
        $table->increments('id');
        $table->string('qualité');
        $table->integer('nbr_heure');
        $table->string('service');
        $table->float('note', 1,1);
        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients');
        $table->integer('intervention_id')->unsigned();
        $table->foreign('intervention_id')->references('id')->on('interventions');
        $table->timestamps();
    });
}
lostdreamer_nl

@chagouani As I said, "I'd check if client_id and intervention_id are actually set in the request."

Change your endpoint to this:

public function store(Request $request)
    {

    dd($request->all());

    // You should really validate your data as well...... Dont ever trust a user.
    // https://laravel.com/docs/5.6/validation
    $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->toArray());
  }

To see what is actually being posted, I believe that either client_id or intervention_id was not being set.

chagouani

@lostdreamer_nl

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
lostdreamer_nl

You said you had an SQL problem:

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation

But this error tells you you're POSTing to a GET route, or GETting a POST route. (MethodNotAllowedException)

First fix your form or ajax request or whatever, then we can get back to the SQL problem.

chagouani

@lostdreamer_nl can you helping me to solved please

Please sign in or create an account to participate in this conversation.