【问题标题】:Updating records in model更新模型中的记录
【发布时间】:2017-11-05 21:45:20
【问题描述】:

我尝试更新数据库中的模型。 我有原始值和值的请求。

'old_log'- 在数据库中查找行的旧值。

 public function UserChange2(Request $request){
        dump($request);
        $data=$request->all();
        $log = $request->input('old_log');
    dump($log);
$user=userModel::select(['fam','im','ot','phone','log','pass'])->where('log',$log)->first();
    $temp=$request->input('fam');
   $user->fam=$temp;
    $temp=$request->input('im');
    $user->im=$temp;
    $temp=$request->input('ot');
    $user->ot=$temp;
    $temp=$request->input('phone');
    $user->phone=$temp;
    $temp=$request->input('log2');
    $user->log=$temp;
    $temp=$request->input('pass');
    $user->pass=$temp;
    dump($user);
    $user->save;
}

但是表中的记录没有更新。

型号:

class userModel extends Model
{
    public $timestamps = false;
    protected $fillable=['fam','im','ot','phone','log','pass','reg','token','date','position'];

}

现在:

   dump($request);
        $data=$request->all();
        $log = $request->input('old_log');
    dump($log);
  $user=userModel::select(['fam','im','ot','phone','log','pass'])->where('log',$log)->first();
           $user->fill($request->all())->save();
        $user->save;

还有错误:

找不到列:1054 'where 子句'中的未知列 'id'(SQL:更新 user_models 设置 fam = y,phone = gggg,pass = tttyyyyyyyy 其中id 为空)

【问题讨论】:

  • 你的模型中有什么 $fillable ?另外,你可以做$user->fill($request->all())->save() 而不是所有的分配
  • ' 公共 $timestamps = false; protected $fillable=['fam','im','ot','phone','log','pass','reg','token','date','position'];'

标签: php laravel laravel-5


【解决方案1】:

您的 userModel 扩展了 Model,这是一个 Eloquent 模型,默认情况下期望相关表有一个名为 id 的主键。

错误

找不到列:1054 'where 子句'中的未知列 'id'

表示id列不存在。

检查您的表定义。这是在您的 Laravel 项目之外创建的数据库中的表吗?

如果没有,检查你的数据库迁移是否定义了主键id。阅读here 了解数据库迁移。

如果已经有一个主键你想使用而不是id,你可以告诉 Eloquent:

protected $primaryKey = 'your_primary_key';

here

【讨论】:

    【解决方案2】:

    您的模型中可能没有完成可填充项。 转到应该为空白的模型并添加可以完成的字段:

    示例:

        protected $fillable = [
        'phone', 'log2', 'pass',    
    ];
    

    你可以在这里的 Laravel 手册中找到这个: https://laravel.com/docs/5.5/eloquent#mass-assignment

    【讨论】:

      【解决方案3】:

      从你的雄辩中删除选择。试试这个。。

      $user=userModel::where('log',$log)->first();
      $user::create($request->all());
      

      编辑

      $user=userModel::where('log',$log)->first();
      $request->request->add(['log' => $request->log2]);
      $user::create($request->all());
      

      【讨论】:

      • 我不能 $request->all()。在我的请求中有旧的 valur 登录,以在数据库中查找对象。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2023-04-02
      • 2014-11-14
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多