I have the following code in my method which I am sending via ajax to the controller method :

    $newUser = \App\UserInfo::updateOrCreate([
        'user_id'   => Auth::user()->id,
        'about'     => $request->get('about'),
        'sec_email' => $request->get('sec_email'),
        'gender'    => $request->get("gender"),
        'country'   => $request->get('country'),
        'dob'       => $request->get('dob'),
        'address'   => $request->get('address'),
        'mobile'    => $request->get('cell_no')
    ]);

The dd($request->all()) gives me :

array:8 [
  "_token" => "fHeEPfTvgMD3FpIBmmc6DmKXFaiuWKZEiOhg6twQ"
  "about" => "Some about me."
  "sec_email" => "example@gmail.com"
  "country" => "Priority highest"
  "gender" => "male"
  "dob" => "12/12/1990"
  "address" => "Some address"
  "cell_no" => "234234234"
]

这太完美了.

Jquery code :

$('#submit-editProfile-form').on('click', function() {
    var profileEditForm = $("#edit-user-profile");
    var formData = $('#edit-user-profile').serialize();
    profileEditForm.on('submit', function(e){
        e.preventDefault();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $.ajax({
            url:'/freelance/edit-userProfile-info',
            type:'POST',
            data:formData,
            error: function (data) {
               console.log('Error');
            }
        });
    }).submit();
});

Now the problem is that i have a record in my table, But the above code creates another one, And the second is that it creates multiply by two records on each button click (request).

推荐答案

In your use case, you should specify a second parameter. The first indicates the conditions for a match and second is used to specify which fields to update.

$newUser = \App\UserInfo::updateOrCreate([
    //Add unique field combo to match here
    //For example, perhaps you only want one entry per user:
    'user_id'   => Auth::user()->id,
],[
    'about'     => $request->get('about'),
    'sec_email' => $request->get('sec_email'),
    'gender'    => $request->get("gender"),
    'country'   => $request->get('country'),
    'dob'       => $request->get('dob'),
    'address'   => $request->get('address'),
    'mobile'    => $request->get('cell_no')
]);

以下是文档中的一个示例:https://laravel.com/docs/5.4/eloquent

// 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]
);

Laravel相关问答推荐

当未通过验证的字段是值数组时,Laravel$validator->;validator()方法不会引发异常

当使用Craftable PRO时,Wysiwig是什么时候生成的?

在Laravel中URL中添加保护ID

如何让 Laravel 的 Collection 表现得像一个流?

在 Laravel 包中的路由上使用显式或隐式模型绑定

如何在生产中的 laravel 应用程序中获取标头请求值?

Laravel - 出于某种原因,max 打破了我的查询

克隆后设置 Laravel 项目

如何在 Laravel 中为模型命名,以免与现有类发生冲突?

如何在 Laravel 5 中验证当前、新密码和新密码确认?

用于集合的 Laravel 5.5 API 资源

Laravel 规则和正则表达式 (OR) 运算符的问题

如何处理 php (Laravel api) 和 javascript (AngularJS) 之间的日期时间

如何在laravel 5.1中使用url(路由)传递多个参数

在 Laravel 5.4 中向多个抄送收件人发送Electron邮件

laravel 5.1 在没有 VM 重启的情况下看不到作业(job)文件的更改

Laravel League/flysystem 使用 AWS S3 获取文件 URL

使用 Carbon 将小时转换为 PM 和 AM

Laravel 验证用户名不允许有空格

错误:找不到模块 'webpack/lib/rules/DescriptionDataMatcherRulePlugin' 需要堆栈: