anonymouse703's avatar

I have a request data but doesn't updated in my database

Hello everyone newbee here... I used Laravel 7 and Vue JS.

I tried to update a record using vue js. When I log the request I have this data

[2020-09-23 04:32:47] local.INFO: array (
  'id' => 9,
  'name' => 'PS Bank',
)  

but my update is not working... this is my controller

public function update(Request $request, MoneyTransfer $moneyTransfer)
    {
        $this->validate($request, [
            'name' => 'required'
        ]);
        
        $data = $moneyTransfer->update($request->all());
        \Log::info($request->all());
    }

Do I need to include id in my fillable in the model? this is the model. protected $fillable = ['name'];

Thank you and God bless

0 likes
21 replies
laracoft's avatar

It is not good to have id as fillable due to security issues. In fact you should add id to $guarded.

Use something like this to solve your issue $data = $moneyTransfer->update($request->all())->where("id", $request->get("id"));

frankielee's avatar

primary key should not be fillable, you can try to code like this:

$validatedData = $this->validate($request, [
            'name' => 'required'
        ]);

  $data = $moneyTransfer->update($validatedData);
        \Log::info($validatedData);

anonymouse703's avatar

I got error 500 Internal Server Error

in the log local.ERROR: Call to a member function where() on bool {"exception":"[object] (Error(code: 0): Call to a member function where() on bool at C:\\wamp64\\www\\bethyl-trade\\app\\Http\\Controllers\\MoneyTransferController.php:88) [stacktrace]

anonymouse703's avatar

I tried your code but it's still not working... do I need to check my route?

Sinnbeck's avatar

What is on line 88 MoneyTransferController

anonymouse703's avatar

sir Laracoft suggestion $data = $moneyTransfer->update($request->all())->where("id", $request->get("id"));

anonymouse703's avatar

I don't know... that's the new thing in Laravel 7. here is the update

public function update(Request $request, MoneyTransfer $moneyTransfer)
    {
        $this->validate($request, [
            'name' => 'required'
        ]);

        $moneyTransfer->update($request->all());
        return ['message' => 'Updated the user info'];
        
    }

But in my other project a pure Laravel that code is working... but when I use Vue it doesn't work.

laracoft's avatar

What is the name of this class? And when you run this?


public function update(Request $request, MoneyTransfer $moneyTransfer)
{
    $this->validate($request, [
        'name' => 'required'
    ]);

    $moneyTransfer->update($request->all())
        ->where("id", $request->get("id"));
        
    return ['message' => 'Updated the user info'];
    
}
frankielee's avatar

If possible, just show your controller and model here.

laracoft's avatar

Your errors says that class MoneyTransfer is a controller. I have no idea if it is a controller or model..... basically class MoneyTransfer

anonymouse703's avatar

I didn't see this sorry... the error of this is here:

app.js:285 PUT http://www.bethyl-trade.test/api/money-trans/13 500 (Internal Server Error)

[2020-09-23 06:54:02] local.ERROR: Call to a member function where() on bool {"exception":"[object] (Error(code: 0): Call to a member function where() on bool at C:\\wamp64\\www\\bethyl-trade\\app\\Http\\Controllers\\MoneyTransferController.php:95) [stacktrace]

anonymouse703's avatar

My model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class MoneyTransfer extends Model
{
    
    protected $primaryKey = 'id';
    
    protected $table = 'moneytrans';

    protected $fillable = ['name'];

}
My Controller
laracoft's avatar

@anonymouse703 can you try this again and paste the error output?

public function update(Request $request, MoneyTransfer $moneyTransfer)
{
    $this->validate($request, [
        'name' => 'required'
    ]);

    $moneyTransfer->update($request->all())
        ->where("id", $request->get("id"));
        
    return ['message' => 'Updated the user info'];
    
}
anonymouse703's avatar

Here is the error Status Code: 500 Internal Server Error

[2020-09-23 07:12:14] local.ERROR: Call to a member function where() on bool {"exception":"[object] (Error(code: 0): Call to a member function where() on bool at C:\\wamp64\\www\\bethyl-trade\\app\\Http\\Controllers\\MoneyTransferController.php:95) [stacktrace]

line 95 ->where("id", $request->get("id"));

laracoft's avatar
laracoft
Best Answer
Level 27

Try

public function update(Request $request, MoneyTransfer $moneyTransfer)
{
    $this->validate($request, [
        'name' => 'required'
    ]);

    $moneyTransfer
        ->where("id", $request->get("id"))
        ->update($request->all());
        
    return ['message' => 'Updated the user info'];
    
}
anonymouse703's avatar

It's working wow..... You save my day sir.. Is it the same way on delete? by the way I have also problem on the swal... I still learning laravel + Vue now.

anonymouse703's avatar

okay i will post it now it on loading the data after update. by the way thank you

Please or to participate in this conversation.