I don't know what return that like method of User model but just remove if statement from your code.
Auth::user()->like($item);
Notifications::create(]
//
]);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I'm modifying my like system a bit, everything works but the query to insert the notification about the like just put, doesn't work. I can't understand why!
This is my code:
function save_like(Request $request){
$item = Items::findOrFail($request->item);
if(Auth::user()->hasLiked($item)) {
Auth::user()->unlike($item);
return response()->json([
'bool'=>false
]);
} else {
$like = Auth::user()->like($item);
if($like == true){
// notification
Notifications::create([
'sender_id' => Auth::id(),
'recipient_id' => Request('recipient_id'),
'item_id' => Request('item'),
'notification_type' => "like",
'like_id' => Auth::user()->likes()->with('likeable')->first()->id,
'seen' => 2,
]);
}
return response()->json([
'bool'=>true
]);
}
}
Please or to participate in this conversation.