1

I cant seem to get my code working 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. If there is a "value" found for that $id then update the value already saved in the DB.

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

   }

    }
Jess McKenzie
  • 8,345
  • 27
  • 100
  • 170
  • If you are using mysql and were willing to prepare a bind statement with [`DB::raw()`](http://fideloper.com/laravel-raw-queries) then you could use a [`REPLACE INTO`](http://stackoverflow.com/questions/19820724/replace-into-query-syntax) statement – Jeff Puckett Apr 30 '16 at 22:09
  • did u find a solution? – Achraf Khouadja May 04 '16 at 03:48

1 Answers1

1

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

Achraf Khouadja
  • 6,119
  • 4
  • 27
  • 39