laravel updateOrCreate方法

时间:2017-03-09 12:54:16

标签: php jquery forms laravel eloquent

我的方法中有以下代码,我通过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();
});

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

3 个答案:

答案 0 :(得分:37)

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

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

答案 1 :(得分:4)

我的代码是

CityProductTemp::updateOrCreate(
    [
        'city_id' => $city_id,
        'product_id' => $product->product_id
    ],
    [
        'mrp' => $mrp,
        'sale_price' => $sale_price,
        'in_offer' => $in_offer,
        'is_featured' => $is_featured,
        'sort_number' => $sort_number
    ]
);
  

id子句

中的未知列where
SQL: UPDATE city_product_temp SET mrp = 66, is_featured = 1, 
     updated_at = 2017-09-15 15:23:17, in_offer = 0 WHERE `id` IS null

答案 2 :(得分: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 = [])