简体   繁体   English

如果数据存在则更新数据库字段

[英]Updating db field if data is present

I cant seem to get my code working 100%.我似乎无法让我的代码 100% 工作。 I am trying to get my code to work in a way like this:我试图让我的代码以这样的方式工作:

if the column value is empty ( $id = columnName ) then insert into that row.如果列值为空( $id = columnName ),则插入该行。 If there is a "value" found for that $id then update the value already saved in the DB.如果找到该$id的“值”,则更新已保存在数据库中的值。

Function:功能:

     $socialCheck = SocialSettings::where($id)->first();

       $socialInsert = new SocialSettings;

       $value        = Request::input('value'); 


       if(SocialSettings::where_not_null($id)){

           $socialInsert->$id = $value;
           $socialInsert->save();
   }else{
        $socialCheck->update($value);

   }

    }

Here is an exemple from a similar question这是来自类似问题的示例

$user = User::firstOrNew(array('name' => Input::get('name')));
$user->foo = Input::get('foo');
$user->save();

Like this像这样

$socialCheck = SocialSettings::firstOrNew(array('id' => $id));
$socialCheck = Request::input('value');
$socialCheck->save();

firstOrNew Laravel API DOCS firstOrNew Laravel API 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM