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

theUnforgiven's avatar

Ok changed to user $notify = Notification::find($input['banner_id'])->user()->attach(User::find($id));

and now get

Creating default object from empty value
mstnorris's avatar

I would suggest keeping it as users() as the plural makes more sense. They are many to many after all.

I'm not sure why that is.

theUnforgiven's avatar

ok changed back the controller method and model to users() but stil same error.

theUnforgiven's avatar

Ah my bad I got it now, I need to amend some of the code and add a read column in the pivot table then it doesn'r show for this user but still shows for everytone else who hasn't read it, sorry been a long day 14 hours so far to exact lol

mstnorris's avatar

Glad I could help. If you run into anything else let me know.

mstnorris's avatar

No problem. Can you do me a favour and mark an appropriate answer as correct (if it is solved). Then open a new thread if you have any more trouble. Otherwise others won't find it useful.

theUnforgiven's avatar

@mstnorris how can i update a new read column to 1 via this command?

Notification::find($input['banner_id'])->users()->attach(User::find($id));

mstnorris's avatar

What do you mean a new read? Do you mean to touch the timestamps?

theUnforgiven's avatar

No i've added a new column called read an that needs updating to 1 as though its been read by the user. As I'm using Notification::all(); to get all notifications but then even when iv'e read it and marked it as read it still displays.

mstnorris's avatar

Instead of doing that as then you are doing twice the amount of work. Why not just check that the Notification isn't in the pivot table?

You have a Notification and a User

When a user has read the notification a record is made in the pivot table (meaning that it has been read). Adding another column is superfluous.

Just do a check for this when you are getting the Notifications.

theUnforgiven's avatar

Ok how based on I'm using the Notifications::all(); method to then do a loop.

mstnorris's avatar

Try this:

$notifications = User::find(Auth::user->id)->notifications()->wherePivot('user_id', '!=', $user->id)->get();
1 like
theUnforgiven's avatar

I get

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pivot' in 'where clause' (SQL: select * from `notifications` where `pivot` = user_id)
theUnforgiven's avatar

Yeah I noticed changed it but still same error Notification::wherePivot('user_id', '!=', Auth::id())->get();

theUnforgiven's avatar

Call to undefined method Illuminate\Database\Eloquent\Collection::wherePivot() is the error now :(

theUnforgiven's avatar

Have I not to use the ->withPivot('foo', 'bar'); property as per the docs?

mstnorris's avatar

@lstables the with pivot is just for extra attributes (if you had have kept the read attribute).

theUnforgiven's avatar

Yes ihave the read attr

CREATE TABLE `notification_user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `read` int(1) DEFAULT NULL,
  `notification_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`),
  KEY `notification_user_notification_id_index` (`notification_id`),
  KEY `notification_user_user_id_index` (`user_id`),
  CONSTRAINT `notification_user_notification_id_foreign` FOREIGN KEY (`notification_id`) REFERENCES `notifications` (`id`) ON DELETE CASCADE,
  CONSTRAINT `notification_user_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
mstnorris's avatar

Can you show me your migration file?

@lstables the code above works for me so I think you have something else going on. Can you try it again?

theUnforgiven's avatar
public function up()
    {
        Schema::create('notification_user', function(Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->integer('read');
            $table->integer('notification_id')->unsigned()->index();
            $table->foreign('notification_id')->references('id')->on('notifications')->onDelete('cascade');
            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
    }
mstnorris's avatar

That looks good. Can you try the code again, just to be sure. Maybe your file hadn't saved and the changes hadn't taken effect.

theUnforgiven's avatar

Same erro Call to undefined method Illuminate\Database\Eloquent\Collection::wherePivot()

on this User::find(Auth::id())->notifications->wherePivot('user_id', '!=', Auth::id())->get();

mstnorris's avatar

You're missing some ()

User::find(Auth::id())->notifications()->wherePivot('user_id', '!=', Auth::id())->get();
theUnforgiven's avatar

Ah good spot, now ive emptied out my pivot table but now no notifitcaions showing

Please or to participate in this conversation.