0

I'm making a adressbook app via laravel 5.2 and vuejs, my app needs CRUD functionality, i stuck at Update part, i send the data via ajax to laravel and i get the data in laravel but i cant update rows. this is my method in vuejs that handle updating:

updatecontact:function(){
    var contactid = this.editingcontact.id;
    var contact update = JSON.stringify(this.editingcontact);
    this.$http({url: '/adressbook/'+contactid, data: {contactupdate} , method: 'PATCH'})
    .then(function (response) {
        console.log(response);
    }, function (response) {
      // error callback
    });

and this the method that handles ajax request in laravel(it's a PUT)

public function update(Request $request, $id)
{
     $adressbook = Adressbook::findorFail($id);
     $adressbook->save($request->all());
}

at last this is how the data looks like:

contactupdate: "{"id":5,"companyName":"poolad","zamineKar":"test","tel":"44044440","fax":"44044422","email"}"
Jerodev
  • 32,252
  • 11
  • 87
  • 108
moein rahimi
  • 774
  • 10
  • 20
  • Have you tried outputting what your laravel application receives? `dd($request->all());`. – Jerodev Feb 11 '16 at 15:45
  • While accessing from laravel are all your data in contact update envelope ? – Nabin Kunwar Feb 11 '16 at 15:51
  • @Jerodev it return this : array:1 [ "contactupdate" => "{"id":5,"companyName":"poolad","zamineKar":"ریخته گری قطعات چدن و فولادی","tel":"44044440","fax":"44044422","email":"info@pooladcrusher.com","adres":"تهران میدان نور ساختمان گلها شرکت پولاد سنگ شکن","created_at":"2016-02-10 09:26:58","updated_at":"2016-02-10 12:30:16"}" ] – moein rahimi Feb 11 '16 at 15:55
  • @nbin yes it is in a contactupdate – moein rahimi Feb 11 '16 at 15:56
  • so why this happen when i want to update? i do the same for post request and my data is in a addcontact envelope – moein rahimi Feb 11 '16 at 16:07

1 Answers1

0

A better way to do it would just be to send this.editingcontact as the data:

updatecontact:function(){
  var contactid = this.editingcontact.id;
  this.$http({url: '/adressbook/'+contactid, data: this.editingcontact , method: 'PATCH'})
  .then(function (response) {
    console.log(response);
  }, function (response) {
  // error callback
});

Then this update code should work:

public function update(Request $request, $id)
{
 $adressbook = Adressbook::findOrFail($id);
 $adressbook->update($request->all());
}
Jeff
  • 24,623
  • 4
  • 69
  • 78
  • I tried both of your suggestions but it's not working, when i dd the save method it returns true but nothing changes. – moein rahimi Feb 11 '16 at 16:52
  • it's working fine when i get data one by one like this: $adressbook->companyName = $request->input('companyName'). but i wonder why $request->all() doesnt work as well – moein rahimi Feb 11 '16 at 17:02
  • One or more of your inputs may not be fillable. in your model, you should have `$fillable = ['companyName',...];` for any fields you want to be able to fill using an array. This should probably not include `id`. So put the fields that you want to be fillable in your model then use `$adressbook->save($request->except('id'));` – Jeff Feb 11 '16 at 17:26
  • the fillable was just fine, i tried except method and also send data without id variable, still the same i'm getting disappoint at why all() method not working – moein rahimi Feb 11 '16 at 18:12
  • @moeinrahimi fix the casing on your findorFail method (should be `findOrFail($id)` and then try using the update method `$adressbook->update($request->all());` – Jeff Feb 11 '16 at 18:20