Steve Boo
Steve Boo

Reputation: 63

Update MongoDB with laravel 5 jenssegers

I tried to make the update document with mongoDB is an update but the problem what I mean is not appropriate. In mongoDB database there are a few users and I want to update user with id: 123 but update not id 123 but first user id 1.

Code:

$user = User:: first ([' _ id ', Input:: get [' id ']]);
$user >-email = ' [email protected] ';
$user >-save ();

How to update that document id = 123?

Upvotes: 0

Views: 7614

Answers (4)

Anmol Mourya
Anmol Mourya

Reputation: 550

This worked for me

$user = User::where('_id',Input::get['id'])
        ->update(['email' => '[email protected]']); 

Upvotes: 1

Morteza Sepehri Niya
Morteza Sepehri Niya

Reputation: 414

  1. Be sure User Model extends Jenssegers\Mongodb\Model
  2. after first line add this line to see what user fetched! dd($user->toArray());
  3. try this: $user = User::where('_id',Input::get['id']);

if #3 worked something in your configuration( for using Jenssegers's package ) is wrong!

Upvotes: 0

Steve Boo
Steve Boo

Reputation: 63

After having many failed attempts, I finally found the right one. And here's the answer:

 $user = UpdateUser::where('_id' , '=' , Input::get('id'))->first();
  $user->username = Input::get('usern');
  $user->save()

;

Upvotes: 2

Morteza Sepehri Niya
Morteza Sepehri Niya

Reputation: 414

$user = User::first(Input::get['id']);
$user->email = '[email protected]';
$user->save();

Upvotes: 1

Related Questions