laravel updateOrCreate method

前端 未结 1 784
独厮守ぢ
独厮守ぢ 2021-01-03 18:29

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

    $newUser = \\App\\UserInfo::updateOrCreate([
        \'user         


        
相关标签:
1条回答
  • 2021-01-03 19:17

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

    Here is an example from the documentation: 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]
    );
    
    0 讨论(0)
提交回复
热议问题