Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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
加密的Laravel模型字段不更新_Laravel_Eloquent - Fatal编程技术网

加密的Laravel模型字段不更新

加密的Laravel模型字段不更新,laravel,eloquent,Laravel,Eloquent,我在更新我的User模型中的github\u oauth\u令牌和github\u oauth\u refresh\u令牌字段时遇到问题,这些字段具有加密的强制转换。所有其他字段更新良好,铸造工作正常,但字段不会保存到数据库中 用户模型 namespace App\Models; use App\Mail\EmailVerification; use Illuminate\Support\Facades\Crypt; use Illuminate\Notifications\Notifiabl

我在更新我的
User
模型中的
github\u oauth\u令牌
github\u oauth\u refresh\u令牌
字段时遇到问题,这些字段具有
加密的
强制转换。所有其他字段更新良好,铸造工作正常,但字段不会保存到数据库中

用户模型

namespace App\Models;

use App\Mail\EmailVerification;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Mail;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name',
        'last_name',
        'email',
        'email_verification_token',
        'password',
        'github_oauth_token',
        'github_oauth_refresh_token',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'github_oauth_token' => 'encrypted',
        'github_oauth_refresh_token' => 'encrypted',
    ];

    /**
     * Set the user's github oauth token.
     *
     * @param string $value
     * @return void
     */
    public function setGithubOauthTokenAttribute($value)
    {
        $this->github_oauth_token = Crypt::encryptString($value);
    }

    /**
     * Set the user's github oauth refresh token.
     *
     * @param string $value
     * @return void
     */
    public function setGithubOauthRefreshTokenAttribute($value)
    {
        $this->github_oauth_refresh_token = Crypt::encryptString($value);
    }
}
用户迁移

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('first_name', 45);
            $table->string('last_name', 45);
            $table->string('email')->unique();
            $table->string('email_verification_token', 30)->nullable();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('github_oauth_token')->nullable();
            $table->string('github_oauth_refresh_token')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
控制器方法

public function handleGitHubCallback()
    {
        $gitHubUser = Socialite::driver('github')->user();

        $user = Auth::user();

        $user->github_oauth_token = $gitHubUser->token; // does not update
        $user->github_oauth_refresh_token = $gitHubUser->refreshToken; // does not update
        $user->first_name = 'Johnathan'; // updates fine
        
        $user->save();

        return redirect()->route('space.list.get')->with('success', Lang::get('messages.success_github_linked'));
    }

问题出现在
用户模型中
。您正在$cast属性中强制转换这两个字段

 protected $casts = [
        'email_verified_at' => 'datetime',
        'github_oauth_token' => 'encrypted',
        'github_oauth_refresh_token' => 'encrypted',
    ];
    
所以不需要添加变异子,所以移除下面两个变异子

public function setGithubOauthTokenAttribute($value)
{
    $this->github_oauth_token = Crypt::encryptString($value);
}

public function setGithubOauthRefreshTokenAttribute($value)
    {
        $this->github_oauth_refresh_token = Crypt::encryptString($value);
    }
另外,如果你想使用变异子,那么你有一个错误,它应该如下所示

public function setGithubOauthTokenAttribute($value)
    {
          $this->attributes['github_oauth_token ']= Crypt::encryptString($value);
    }

    public function setGithubOauthRefreshTokenAttribute($value)
        {
           $this->attributes['github_oauth_refresh_token ']= Crypt::encryptString($value);
        }

正如你所建议的,我移除了突变子,但那不起作用,导致了同样的行为。我从casts数组中删除了字段,它(通过mutators)工作了。理想情况下,我只想使用加密强制转换,但它在我的情况下似乎不起作用…好的,在mutator中,你有错误,正如我所说的,我已经测试了这两种情况,它对我很有效。任何一种你必须使用的。不是强制转换就是变异