Summer Developer
Summer Developer

Reputation: 2086

Laravel Function Not Updating Database

I am calling the function below

 function makeanote($type,$id){
$note = Notification::where("user_id",Auth::user()->id)->get();
if ($type == "user"){
if($id > $note->pluck("users") ){
$note->users = $id;
$note->save();  
return;
}   
}
return;
           }

Like so: makeanote($type,$id). The calling $type is "user" and the calling $id is "31".

In the database for my current user, the $note value at the users column is currently zero (0).

Therefore I would expect it to update to 31, but it is staying at zero. Am I using pluck() incorrectly?

Thank you.

Upvotes: 0

Views: 20

Answers (1)

IzzEps
IzzEps

Reputation: 582

Give this a shot:

function makeanote($type,$id){
$note = Notification::where("user_id",Auth::user()->id)->first();
if ($type == "user"){
if($id > $note->users){
$note->users = $id;
$note->save();  
return;
}   
}
return;
       }

My changes are: ->first() (this will return a single object) instead of ->get() (this returns a object collection) and once you have the object you can access the users attribute directly.

Upvotes: 1

Related Questions