update () and destroy () methods do not work in Laravel API RestFul

0

I'm doing a RestFul API with Laravel, and everything goes well with the methods, except the method update() and the method destroy() , ( PUT and DELETE respectively), it's not a problem of CORS , if it enters the function and that verifies that when using the respective verbs HTTP enters the corresponding function, only that it is not executing what is done inside it

I leave the code

<?php

namespace App\Http\Controllers;

use App\Client;

use Illuminate\Http\Request;

use App\Http\Requests;

use Illuminate\Support\Facades\Input;



class ClientController extends Controller
{
    //
    public function index()
    {
        return Client::paginate(5);
    }

    public function store()
    {
        return Client::create(Input::all());
    }


    public function show($id)
    {
        return Client::findOrFail($id);
    }


    public function update($id)
    {
        Client::findOrFail($id)->update(Input::all()); 

        return Client::findOrFail($id);
    }


    public function destroy($id)
    {
        $deleted = Client::findOrFail($id);
        Client::findOrFail($id)->delete();

        return $deleted;
    } 
}

and here the code of my route.php

Route::resource('clients', 'ClientController');
    
asked by Anthony Medina 14.01.2018 в 06:22
source

2 answers

0

Do not use findOrFail () I think you got the problem there. Be sure to try this

public function update(Client $client)
{
    $client->update(Input::all()); // Yo uso el Request $request no el Input pero no debería ser el problema
    return $client;
}

public function destroy(Client $client)
{
    $client->delete();
    return $deleted;
} 

If not try optimizing your code, in each method you are making 2 calls to the database

public function update($id)
{
    $client = Client::findOrFail($id);
    $client->update(Input::all()); 

    return $client;
}

public function destroy($id)
{
    $client = Client::findOrFail($id);
    $client->delete();

    return $client;
} 
    
answered by 10.03.2018 в 11:11
0

To see what happens, In routes / api.php I added this listener so that it prints you in storage / logs / laravel.log the SQL instructions

\Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {

    Log::debug($query->sql);
    Log::debug($query->bindings);
    Log::debug($query->time);

});

In the model:

protected $fillable = ['name', 'description'];

Update:

    $client = Client::findOrFail($id);
    $client->fill($request->all());
    $client->save();
    
answered by 10.03.2018 в 11:42