简体   繁体   English

laravel如何将字段更新到数据库

[英]laravel how to update a field to a database

This is my code 这是我的代码

$admin = Admin::find(25);
        $admin->picture = $destination;
        $admin->save();

but when I execute it, the picture field is keep NULL for example images/admin_20 但是当我执行它时, picture字段保持NULL ,例如images/admin_20

the destination is a string to the location of the image. destination是图像位置的字符串。

I already tried ->update() but also nothing becomes saved. 我已经尝试过->update()但是什么也没有保存。

could you help me please 请问你能帮帮我吗

Edit 1 编辑1

the picture column in my database is varchar(255) utf8_unicode_ci 我数据库中的picture列是varchar(255) utf8_unicode_ci

Try this: 尝试这个:

     $admin = Admin::where('id', '=', 25);
     $admin->picture = $destination;
     $admin->save();

You didn't provide much code but if it's a function 您没有提供太多代码,但是如果它是一个函数

      Return $admin->picture;

EDIT 编辑

Laravel is UTF8 by default but just to make sure, check the file Laravel默认为UTF8,但要确保检查文件

 /etc/mysql/my.cnf
 collation-server = utf8_unicode_ci
 init-connect='SET NAMES utf8'
 character-set-server = utf8

Finally I found the problem, three days working to this silly mistake 终于我找到了问题,为这个愚蠢的错误工作了三天

the ID column in my database should have been id 我数据库中的ID列应为id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM