Change it to users()
Ok changed to user $notify = Notification::find($input['banner_id'])->user()->attach(User::find($id));
and now get
Creating default object from empty value
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.
ok changed back the controller method and model to users() but stil same error.
But the pivot table has been updated correctly.
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
Glad I could help. If you run into anything else let me know.
@mstnorris will do i'm sure, cheers buddy for your time and help
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.
Yes will do.
@mstnorris how can i update a new read column to 1 via this command?
Notification::find($input['banner_id'])->users()->attach(User::find($id));
What do you mean a new read? Do you mean to touch the timestamps?
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.
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.
Ok how based on I'm using the Notifications::all(); method to then do a loop.
Try this:
$notifications = User::find(Auth::user->id)->notifications()->wherePivot('user_id', '!=', $user->id)->get();
I get
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pivot' in 'where clause' (SQL: select * from `notifications` where `pivot` = user_id)
@lstables try again, I had an extra s in Notifications::where....
Yeah I noticed changed it but still same error Notification::wherePivot('user_id', '!=', Auth::id())->get();
I've changed it again, try that.
Call to undefined method Illuminate\Database\Eloquent\Collection::wherePivot() is the error now :(
Have I not to use the ->withPivot('foo', 'bar'); property as per the docs?
@lstables the with pivot is just for extra attributes (if you had have kept the read attribute).
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;
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?
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();
});
}
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.
Same erro Call to undefined method Illuminate\Database\Eloquent\Collection::wherePivot()
on this User::find(Auth::id())->notifications->wherePivot('user_id', '!=', Auth::id())->get();
You're missing some ()
User::find(Auth::id())->notifications()->wherePivot('user_id', '!=', Auth::id())->get();
Ah good spot, now ive emptied out my pivot table but now no notifitcaions showing
Please or to participate in this conversation.