Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel雄辩的更新不更新一个字段_Php_Laravel_Vue.js_Eloquent - Fatal编程技术网

Php Laravel雄辩的更新不更新一个字段

Php Laravel雄辩的更新不更新一个字段,php,laravel,vue.js,eloquent,Php,Laravel,Vue.js,Eloquent,我正在使用LaravelAPI和VueJS前端应用程序。我发送了axiosPOST电话以及一些用户关于自己的信息。在后端,我获取数据并更新MySQL数据库中的用户记录 这是我的后端代码: $user = User::where(['logintoken' => $request->login_token, 'remember_token' => $request->remember_token])->first(); if (!$user)

我正在使用LaravelAPI和VueJS前端应用程序。我发送了
axios
POST
电话以及一些用户关于自己的信息。在后端,我获取数据并更新MySQL数据库中的用户记录

这是我的后端代码:

$user = User::where(['logintoken' => $request->login_token, 'remember_token' => $request->remember_token])->first();
            if (!$user) {
                $status = 404;
                $response = ['status' => false, 'message' => 'User not found.'];
            } else {
                $affectedRows = $user->update([
                    'country' => $request->input('country'),
                    'city' => $request->input('city'),
                    'state' => $request->input('state'),
                    'dob' => $request->input('dob'),
                    'language' => $request->input('language'),
                    'street' => $request->input('street'),
                    'house' => $request->input('house'),
                    'avatar' => $request->input('avatar'),
                    'birthplace' => $request->input('birthplace'),
                    'type' => $request->input('userType'),
                    'bio' => $request->input('bio'),
                    'zip' => $request->input('zip'),
                    'has-completed-profile' => 1
                ]);
                if (!$affectedRows) {
                    $response = ['status' => false, 'message' => 'Error updating'];
                    $status = 500;
                } else {
                    $response = ['status' => true, 'message' => 'User updated successfully', 'updated_record' => $user, 'request' => $affectedRows, 'zip' => $request->input('zip')];
                }
            }
现在,这段代码运行得非常好,只是它没有更新
zip
字段。我查看VueJS端的响应
console.log()
,我发现
zip
值是正确的,DB中的数据类型fo
zip
字段也是字符串

不过,这对
zip
列不起作用。当我创建
users
迁移时,没有
zip
列,但过了一段时间,当我需要它时,我创建了另一个迁移,并使用它来更改表以插入
zip
字段


知道它为什么不工作吗?

您需要在相应的模型类中添加该字段

这应该能解决你的问题

$fillable
用作应可批量分配的属性的“白名单”。因此,要开始,您应该定义要使体量可分配的模型属性。您可以使用模型上的
$filleble
属性执行此操作。例如,让我们将
User
模型的
zip
属性设置为可分配的:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['zip', // your rest column names];
}

您可能错过了型号的
$filleble
中的
zip

所以加上它,它应该是这样的:

protected $fillable = [
        'country', 'city', 'state', 'dob', 'language', 'street', 'house', 'avatar', 'birthplace', 'type', 'bio', 'zip', 'has-completed-profile'
    ];

您需要在iTa的Model类中添加该列您是否在您的
用户
模型中使用空的
$guarded
数组?或者,如果使用
$filleble
,则需要在允许的字段数组中添加
zip
。请尝试
Log::info($request->all())
并在
/storage/logs
中检查日志,以验证zip是否正在传递到请求中。@RahulMeshram是的,我真是个笨蛋,忘了这一点!您应该将其添加为答案,LOL。您可以共享.blade视图代码吗?正如@nakvov所提到的,验证zip是否列在$fillable中?如何在控制器中添加值?如果您有实体模型,那么为什么要在控制器中添加它。?