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

thebigk's avatar
Level 13

How does this query work? Is it efficient?

I am looking at filtering my database notifications with an attribute in my data column. We're talking about the default notifications table created by Laravel.

I was (pleasantly) surprised to see that the following query actually works! :

auth()->user()->unreadNotifications->where('data.tenant_id', 1);

I'm wondering if this query is efficient if my data column is text? Should I go about making it json?

But before coming up with above solution, my attempt was this -

$notifications = auth()->user()->unreadNotifications()->get();

        // Only pick the notifications that belong to current tenant

        $notifications->filter(function($value) use($request) {
           return $value->data['tenant_id'] = $request->tenant->id;
        });

       

This code threw error -

Indirect modification of overloaded property Illuminate\Notifications\DatabaseNotification::$data has no effect

What's going on here?

0 likes
1 reply
thebigk's avatar
Level 13

Pfft!

Turns out that data.tenant_id isn't being searched in the database. It's simply filtering the collection retrieved from the database.

However, I'm now facing query with marking the notifications as read based on data.tenant_id; without running multiple queries on each notification.

Please or to participate in this conversation.