laravel updateorCreate method

2022-08-30 09:34:21

我的方法中有以下代码,我通过ajax发送到控制器方法:

    $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')
    ]);

给我 :dd($request->all())

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 代码 :

$('#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();
});

现在的问题是,我的表中有一条记录,但是上面的代码创建了另一个记录,第二个是它在每个按钮单击(请求)时创建乘以两条记录。


答案 1

在您的用例中,您应该指定第二个参数。第一个指示匹配的条件,第二个用于指定要更新的字段。

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

答案 2

Eloquent 有一个名为 的方法,可以像这个例子一样使用:updateOrCreate

<?
$flight = 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')
    ],
);
  1. 搜索依据可能是识别行的最佳方式。user_id
  2. 它根据第二个数组中给出的值创建或更新。

以下是该方法的符号。

/**
 * Create or update a record matching the attributes, and fill it with values.
 *
 * @param  array  $attributes
 * @param  array  $values
 * @return \Illuminate\Database\Eloquent\Model
 */
public function updateOrCreate(array $attributes, array $values = [])

推荐