Muthu
Muthu

Reputation: 209

Laravel how to retrieve the recently uploaded values

I have a database in mysql. I upload a set of data to the database table from a .csv file or .xls file. During retrieval I need to retrieve only the set of values which I have recently uploaded to the table in the database. I should not get all the values which stored in the table.(only values recently uploaded to the table should be retrieved). How can I do that using Laravel?

Upvotes: 0

Views: 250

Answers (4)

lesssugar
lesssugar

Reputation: 16181

(1)

In the table where you store the data from CSV and XML, using a migration, create a flag to mark the records you didn't process yet, e.g. is_processed. Give it the default value of 0.

(2)

If you have data you already retrieved (processed), set those to 1, so they don't get processed again. You can do this manually with a query, based on the conditions that make sense in your case.

(3)

Then, after you store new data, they will be automatically set to is_processed = 0. This means you can identify them afterwards, like this:

$unprocessedData = MyModel::where('is_processed', 0)->get();

You can also order them if you need to, but - in general - that's the approach I would take.

Upvotes: 1

Kuldeep Mishra
Kuldeep Mishra

Reputation: 4040

$latest= Users::orderBy('created_at','desc')->take(10)->get();

this will return 10 recent values

Upvotes: 1

Asur
Asur

Reputation: 4017

Well it depends on what you consider "recent" but you could tweak a function like this to fit your needs:

->where('created_at', '<', Carbon::now()->subMinutes(5)->toDateTimeString())

This would retrieve the elements created the last 5 minutes, but Carbon offers a wide variety of substractors.

PD: Remember to add use Carbon\Carbon; under your namespace declaration.

Hope this helps you.

Upvotes: 1

Leo
Leo

Reputation: 7420

Lets say you have a table/model named Users;

$getLatestUsers=Users::orderBy('created_at','desc')->take(5)->get();

this will take 5 recent records from database.

Upvotes: 1

Related Questions