Laravel Eloquent many to many filter on intermediate table

Ben Fransen

I'm using a User/Post example in the question. I have two tables Users and Posts linked together with a many to many relationship with post_views as table. Also I've created an Event and Listener to handle the views on a post. But I only want to add a new view-entry if the last view for a given post was over an hour ago by that user.

At the moment my handle() method in the UpdatePostViews listener does:

public function handle(PostWasViewed $event)
{
    $event->post->views()->attach($event->user->id);
}

The relationship is defined with withTimestamps But how can I filter in the handle() method to do something like

    $lastView = User::where('user_id', $userId)
->where('post_id', $postId)
->in('post_views')
->orderBy('id', 'DESC')
->first();

which would return the last row for a user/post combination from the post_views so I can determine on the timestamps it was inserted over an hour ago and add a new entry or skip creation of a new entry.

patricus

You can use the wherePivot method on the BelongsToMany relationship to add constraints on the pivot table for the query.

Once you do that, you can use any combination of logic to determine if there was a view in the last hour. Something like:

public function handle(PostWasViewed $event)
{
    $inLastHour = $event->user->posts()->where('id', $event->post->id)->wherePivot('updated_at', '>=', new \Illuminate\Database\Query\Expression('NOW() - INTERVAL 1 HOUR'))->exists();

    // or, either way, shouldn't matter

    $inLastHour = $event->post->views()->where('id', $event->user->id)->wherePivot('updated_at', '>=', new \Illuminate\Database\Query\Expression('NOW() - INTERVAL 1 HOUR'))->exists();

    if (!$inLastHour) {
        $event->post->views()->attach($event->user->id);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel - Eloquent relation - many-to-many - get intermediate table columns

From Dev

Many to many Laravel Eloquent relationship with multiple intermediate tables

From Dev

Laravel eloquent - Many to Many, select only matching the many table

From Dev

laravel 5.2 many to many relation retrieve data with intermediate table

From Dev

Laravel eloquent ,has many in multi table

From Dev

Laravel 5 Eloquent Many to Many 2ndary table

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

Laravel Eloquent many-to-many relationship: Use explicit pivot table

From Dev

Laravel Eloquent Many to Many Query

From Dev

Laravel many to many selecting with Eloquent

From Dev

Laravel eloquent many to many to many model

From Dev

Laravel Eloquent one to many

From Dev

How to make Laravel eloquent request with 1 filter on many fields

From Dev

Laravel 5 Eloquent ORM - Many to Many through Many to Many

From Dev

Eloquent - Many to many where not found in the pivot table

From Dev

Laravel Eloquent many to many how to add where?

From Dev

Laravel 4 Many to Many with Eloquent and checkboxes

From Dev

Laravel Eloquent Many-To-Many distinct

From Dev

Laravel many to many relationship using Eloquent

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel Eloquent Many-to-Many query

From Dev

Laravel Eloquent "Many-to-Many-to-One ?"

From Dev

Laravel Eloquent Many-To-Many distinct

From Dev

Laravel Eloquent join three table-has many through

From Dev

laravel many to many with third table

From Dev

Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

From Dev

Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5

From Dev

How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

From Dev

a many to many relation.. is it always better to do an intermediate table?

Related Related

  1. 1

    Laravel - Eloquent relation - many-to-many - get intermediate table columns

  2. 2

    Many to many Laravel Eloquent relationship with multiple intermediate tables

  3. 3

    Laravel eloquent - Many to Many, select only matching the many table

  4. 4

    laravel 5.2 many to many relation retrieve data with intermediate table

  5. 5

    Laravel eloquent ,has many in multi table

  6. 6

    Laravel 5 Eloquent Many to Many 2ndary table

  7. 7

    Laravel Eloquent Many to Many relationship using a pivot table

  8. 8

    Laravel Eloquent many-to-many relationship: Use explicit pivot table

  9. 9

    Laravel Eloquent Many to Many Query

  10. 10

    Laravel many to many selecting with Eloquent

  11. 11

    Laravel eloquent many to many to many model

  12. 12

    Laravel Eloquent one to many

  13. 13

    How to make Laravel eloquent request with 1 filter on many fields

  14. 14

    Laravel 5 Eloquent ORM - Many to Many through Many to Many

  15. 15

    Eloquent - Many to many where not found in the pivot table

  16. 16

    Laravel Eloquent many to many how to add where?

  17. 17

    Laravel 4 Many to Many with Eloquent and checkboxes

  18. 18

    Laravel Eloquent Many-To-Many distinct

  19. 19

    Laravel many to many relationship using Eloquent

  20. 20

    Laravel 5 Eloquent count many to many relationship

  21. 21

    Laravel Eloquent Many-to-Many query

  22. 22

    Laravel Eloquent "Many-to-Many-to-One ?"

  23. 23

    Laravel Eloquent Many-To-Many distinct

  24. 24

    Laravel Eloquent join three table-has many through

  25. 25

    laravel many to many with third table

  26. 26

    Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

  27. 27

    Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5

  28. 28

    How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

  29. 29

    a many to many relation.. is it always better to do an intermediate table?

HotTag

Archive