Create posts for a specific age and show them to the right users

When a user creates a post, he can select an age range which users can see this post. for example minimum age 18 maximum age 52. How do I save the data in the best of the post table that I can spend the posts as over pushed down?

If a user registers, he must specify his date of birth. This is stored in the users table under birthday. How can I display the created post only users who have the appropriate age?

1 answer

  • answered 2018-10-11 21:03 Marco Chirico

    The best way is for sure to save the date range on the posts table, or if you want create more flexible application you can create a cluster of age ranges and refer one of them to the post. Of course if you want to adopt a smart and quickly solution, I suggest you to use the first approach (age start and age end on the post table).

    Just to better explain this concept in real code, below you can find an example of data extraction, filtered.

    //user registered age
    $userAge = 52;
    
    //Post: is a "posts_table" model
    $posts = Post::where('age_start','<=', $userAge)->where('age_end','>=', $userAge)->get();
    
    if(count($posts)>0) {
        foreach($posts as $p) {
            print_r($p); //this object contain the entire post record
        }
     }
    

    Hope is helpful for you.