skip to Main Content

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',
        ];
    }

4

Answers


  1. Chosen as BEST ANSWER

    I Did Solve it this is my code in controller file

    public function update(StorePersonRequest $request, $id)
        {
            $input = Person::find($id); 
            $input->CoPe = $request->get('CoPe');
            $input->NaPe = $request->get('NaPe');
            $input->JoPe = $request->get('JoPe');
            $input->TelPe = $request->get('TelPe');
            $input->FaxPe = $request->get('FaxPe');
            $input->PhoPe1 = $request->get('PhoPe1');
            $input->PhoPe2 = $request->get('PhoPe2');
            $input->PlPe = $request->get('PlPe');
            $input->NotPe = $request->get('NotPe');
            $input->EmPe = $request->get('EmPe');
            $input->DaNPe = $request->get('DaNPe');
            $input->Corq = $request->get('Corq');
            $input->FaPe = $request->get('FaPe');
            $input->MaPe = $request->get('MaPe');
            $input->BirthPe = $request->get('BirthPe');
            $input->NoNuPe = $request->get('NoNuPe');
            $input->NoNu1Pe = $request->get('NoNu1Pe');
            $input->CfPe = $request->get('CfPe');
            $input->TyPe = $request->get('TyPe');
            $input->GePe = $request->get('GePe');
            $input->Creator = $request->get('Creator');
            $input->AttPe = $request->get('AttPe');
            $input->Co = $request->get('Co');
            $input->AttF = $request->get('AttF');
            $input->AttL = $request->get('AttL');
            $input->save();
            return response()->json([
            "success" => true,
            "message" => "updated successfully.",
            "data" => $input
        ]);
        }
    

  2. 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

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. Do you have the route built out in the route folder? Either api.php or web.php like below?

    Route::get('tasks', 'AppHttpControllersTaskController@index'); //get tasks with authenticated user_id where is_last_status is 0 with     
        Route::get('tasks/user/{user_id}', 'AppHttpControllersTaskController@tasksForUser'); //get tasks for specific user_id 
        Route::get('tasks/{task}', 'AppHttpControllersTaskController@show'); //get tasks with specified task id number in {task}
        Route::put('tasks/{task}', 'AppHttpControllersTaskController@update');
        Route::post('tasks','AppHttpControllersTaskController@store');
        Route::delete('tasks/{task}', 'AppHttpControllersTaskController@destroy');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search