Search code examples
phpmysqllaravellaravel-query-builder

How to retrieve records from database table in Laravel and prevent those which are expired


So I have a table in MySQL which looks like this

enter image description here

these are basically ads that are posted by users. Duration column in this table is about for how many days the User wants to show their ad. that means if user choose to post ad for 3 days i should not just show after 3 days that means it will expire after three days. Now i Want to retrieve only those records which are not expired. I have searched a lot but could not find a way to do this. Kindly give me a solution or refer to a link where i can possibly find the solution.

Right now I am using this script to get the records with simple pagination

$items=Post::with('items','users')->paginate(12);
$categories = DB::table('item_categories')->get();
return view('search_post', compact('items','categories'));

Solution

  • So I changed my table to this

    Changed Table to this

    And using this script

    $items=Post::with('items','users')->where('duration','>=',\Carbon\Carbon::now() )->paginate(12);
    $categories = DB::table('item_categories')->get();
    return view('search_post', compact('items','categories'));
    

    It is working perfectly. Thanks to every one.