Home > Mobile >  Laravel PUT Request
Laravel PUT Request

Time:10-20

I have a problem when i am trying to update in my table it wont updated and return data []

enter image description here

Function update in Controller

public function update(StorePersonRequest $request, Person $person)
    {
        $person->update($request->all());

        return new PersonResource($person);

    }

StorePersonRequest :

class StorePersonRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'CoPe'=>'required',
            'NaPe'=>'required',
            'PhoPe1'=>'required',
            'Corq'=>'required',
        ];
    }

CodePudding user response:

I remember having the same issue with postman. The problem is not on laravel but on the headers of your request. Select the application/x-www-form-urlencoded for headers and your body will be sent successfully.

Also in the method at the top select PUT instead of POST and remove the _method field of your body

CodePudding user response:

First of all, if you are not passing any file or something, I am not sure whether its good practice to use form-data. You could have just used raw and pass a json with your data using PUT OR PATCH. Also I see that you are passing an id also, any reason for that?

Anyway, I think that all you have to do to make it works, is to put the _method on your url. So in your case it should be like this:

localhost:8000/api/pe/2?_method=PATCH

(or PUT if you prefer it). Right now you are not passing it as a parameter, you include it in your body request. I believe that this is your problem.

CodePudding user response:

Do you have the route built out in the route folder? Either api.php or web.php like below?

Route::get('tasks', 'App\Http\Controllers\TaskController@index'); //get tasks with authenticated user_id where is_last_status is 0 with     
    Route::get('tasks/user/{user_id}', 'App\Http\Controllers\TaskController@tasksForUser'); //get tasks for specific user_id 
    Route::get('tasks/{task}', 'App\Http\Controllers\TaskController@show'); //get tasks with specified task id number in {task}
    Route::put('tasks/{task}', 'App\Http\Controllers\TaskController@update');
    Route::post('tasks','App\Http\Controllers\TaskController@store');
    Route::delete('tasks/{task}', 'App\Http\Controllers\TaskController@destroy');
  • Related