1

i am trying to update text when i send request to Laravel controller, but i am getting error:

500 internal server error 
Uncaught (in promise) Error: Request failed with status code 500

console.log is showing text and values which i am trying to send to controller:

id:16 text:aa

vue js code:

const onEdit=(todo)=>{
    //  var src = evt.target.innerHTML
    //  this.txt = src
    axios
        .put('http://127.0.0.1:8000/api/todo/update', {
            id:todo.id,
            text:todo.text
        })
        .then(() => getTodos());

    console.log("working", todo.text, todo.id);
};

Laravel COntroller:

public function updateData(Request $request)
{
    $todo = Todo::find($request->id);
    $todo->update($request->all());

    return response()->json([
        'message' => 'updated'
    ]);
}

Route:

Route::put('todo/update',[TodoController::class,'updateData']);

Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Todo extends Model
{
    use HasFactory;
    protected $dates = [
        'created_at',
        'updated_at',
        
    ];

    protected $fillable = ['id'];
}

enter image description here

UPDATE:

i checked laravel log and i got error:

Add [id] to fillable property to allow mass assignment on [App\Models\Todo]. 

Then i added id in model. now i am seeing below response in console log but it is not updating data.

enter image description here

7
  • 2
    try $todo->update(['text'=>$request->text]); instead of $todo->update($request->all()); Commented Nov 25, 2021 at 9:54
  • record not updating using it Commented Nov 25, 2021 at 9:59
  • 1
    can you show Todo model code Commented Nov 25, 2021 at 10:00
  • 1
    remove fillable property protected $fillable = ['id']; and add protected $guarded=['id']; Commented Nov 25, 2021 at 10:06
  • 1
    try all () it should work Commented Nov 25, 2021 at 10:11

2 Answers 2

2

The issue is with Todo Model,where you allowed id field as fillable. Since id is auto increment filed in database .So text field should be fillable.

  protected $fillable = ['id'];

so here you have two options .One is to allow $fillable property to text field or guarding id property so all other properties are fillabe

So

 protected $guarded=['id'];

or

protected $fillable=['text'];
Sign up to request clarification or add additional context in comments.

Comments

2

A 500 error is a generic server side error.

Firstly, edit your onEdit function to add a catch so you can console log the server error.

const onEdit = (todo) => {
  axios.put('http://127.0.0.1:8000/api/todo/update', {
    id: todo.id,
    text: todo.text
  }).then(response => {
    console.log(response)
    getTodos()
  }).catch(error => {
    console.log(error)
  })
}; 

Once you know what the server error is then you'll know what to fix.

5 Comments

added snippet under
Ok that's not any help. You'll need to check your Laravel logs too.
Now the data is not updating - this means that your getTodos() functions likely isnt working properly. If you console log the response data from getTodos does it contain your updated todo?
i can't see updated data in sql aswell
There must be something wrong with your ` $todo->update` statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.