Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Marcolino922's avatar

Eloquent Query Doesn't work

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

0 likes
5 replies
MichalOravec's avatar

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(]
    //
]);
Marcolino922's avatar

If the if condition is removed, the likes are not inserted.

Marcolino922's avatar

Yes, the javascript part stops working giving me an error 500 "500 (Internal Server Error)". By updating the page, however, the like is inserted, so the query works but the notification still does not.

Marcolino922's avatar

I've tried all sorts of queries on other tables, just to try, but they aren't logged. I placed the example queries anywhere in the function (I thought maybe something conflicted), but nothing.

So I don't explain what happens, it's since I entered


return response()->json([
                'bool'=>true
            ]);

that doesn't work.

Please or to participate in this conversation.