【问题标题】:Laravel not working method updateOrCreateLaravel 不工作方法 updateOrCreate
【发布时间】:2020-08-06 01:28:26
【问题描述】:

用户有个人资料设置。我希望用户更改要更新的某些字段。但我创建了一个新列,应该更新。也许有人做得不对。请帮忙。非常感谢。

控制器

     public function profile_settings_post(Request $request){
          // Auth Specialist
          $user = Auth::user();
          // Data Specialist Validate
          $data = $request->validate([
              'first_name' => 'nullable|string',
              'last_name' => 'nullable|string',
              'phone_number' => 'nullable|integer',
              'gender' => 'nullable',
              'date_of_birth' => 'nullable',
              'about_me' => 'nullable',
              'address' => 'nullable',
              'city' => 'nullable|string',
              'country' => 'nullable|string',
              'postal_code' => 'nullable|integer',
          ]);
            $profile = $user->profile_settings()->updateOrCreate($data);
            $profile->save();

          // RETURN REDIRECT PROFILE SETTINGS INDEX
        return redirect()->route('frontend.specialist.profile.settings');
    }

用户模型

 class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * 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',
    ];

    public static function countPercent($count, $maxCount){
        //one percent
        $one_percent = $maxCount / 100;
        // $count how much is percent
        $percent = $count / $one_percent;
        return $percent;
    }

    // 1 User have 1 profile settings (ONE TO ONE)
    public function profile_settings(){
        return $this->hasOne(Profile_Settings::class);
    }

}

Profile_Settings 模型:

 class Profile_Settings extends Model
{
    // Fill in db
    protected $fillable = [
        'first_name', 'last_name', 'phone_number',
        'gender', 'date_of_birth', 'about_me',
        'address', 'city', 'country', 'postal_code',
    ];

    // Profile settigns model belongs to User
    public function user(){
        return $this->belongsTo(User::class);
    }
}

当我编辑某种字段时。在数据库中创建一个新字段

配置文件设置数据库不工作更新创建新列

【问题讨论】:

  • 如果我理解正确,在更新配置文件设置时,您将使用更新的数据创建新记录,而不是使用更新的数据更新旧记录,因此您最终会得到两条旧记录/未更新)-(新/更新)对吗?
  • 删除$profile->save(); 行。 updateOrCreate() 已经将数据保存到数据库中。

标签: php laravel


【解决方案1】:

您可能没有仔细阅读 updateOrCreate 的工作原理 它根据您传入的条件执行更新并更新您想要的字段,因此您必须传递 2 个数组。

来自 Laravel 网站的示例

// If there's a flight from Oakland to San Diego, set the price to $99.
// If no matching model exists, create one.
$flight = App\Flight::updateOrCreate(
    ['departure' => 'Oakland', 'destination' => 'San Diego'],
    ['price' => 99, 'discounted' => 1]
);

这意味着我们正在更新所有行,其中“出发”=“奥克兰”,“目的地”=“圣地亚哥”并将价格设置为 99 美元。

你的情况你应该决定条件,当你应该执行更新查询时,它将是第一个数组,并且还决定应该更新哪些字段,将它放在第二个数组中。

【讨论】:

  • 你能帮忙吗?我不能。我尝试了所有选项都一样不起作用。
  • 请描述具体应该更新的时间和内容(条件和字段),我会尽力提供帮助。
  • 我会告诉你我的任务。用户有一个设置配置文件模型。当新字段的配置文件尚未填写时。当它被填充时,会显示基础中的列。但也许随着时间的推移有些事情会发生变化。我需要更新同一列,而不是创建新列。
猜你喜欢
  • 2017-07-30
  • 1970-01-01
  • 2022-11-07
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 2018-11-21
相关资源
最近更新 更多