Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

Show statistics of what and how many times a user downloads something

How can I produce something that will show what a user has downloaded and how may times?

Is there a package to do this? Or an easy way/example.

Help greatly appreciated.

0 likes
53 replies
Barryvdh's avatar

You can create a route to return a specific file:

Route::get('downloads/{id}', function($id){
    $download = Download::find($id);
    $download->count++;
    $download->save;
    return Response::download($download->filename);
});

You could probably also use a redirect to the real file location.

3 likes
Barryvdh's avatar

Can't you link to the route instead, to make that return the file?

bashy's avatar

I also use the route method so I can control permissions to files. Really worth doing. You can also hide your files above the public folder so it's not directly accessible.

1 like
theUnforgiven's avatar

Right I understand I'll rewrite this to reflect the route method idea thanks guys

bashy's avatar

Long as you set the right path and you have the exact filename in the database (or full path in database) it should be a quick change.

Shovels's avatar

Alternatively you could attach a Google Analytics event which is fired when the user clicks the download link.

Barryvdh's avatar

Yes but that would only work if the link is clicked on the website, not when shared (eg. email/twitter etc)

bashy's avatar

Yeah wouldn't rely on GA, some people block it etc

Shovels's avatar

@bashy Definitely not something you'd rely on, but good for analysing sources of downloads and other metrics which although not impossible to implement comes out of the box with GA - Although, going back to Barry's point wouldn't track direct downloads (or GA blocked by user).

theUnforgiven's avatar

Not sure I can actually do the way @Barryvdh stated, but working on it. Simply because I have a data table that as an ID and Document columns but there's not a way to get/find the ID as this is showing I'm wanting to show in the dashboard area of the cms I've built.

theUnforgiven's avatar

And the documents don't belong to a user or anything so it would be a case of Data::all() but need to get the amount of times each file has been downloaded and who by.

xingfucoder's avatar

Hi @lstables, Wouldn't it be better to manage it via Ajax requests that control during the OnClick event of the link and updating by a controller method?

Shovels's avatar

Plus you'd have the same issue of not tracking the direct downloads, unless you implement a system as suggested by Barry.

xingfucoder's avatar

Using jQuery you can make any operation stopping the click default event as you need with:

$('#yourLinkId').on('click', function(e) {
    e.preventDefault() //this stop the default click event
    //Here you can put your ajax update operations including queries to your Laravel PHP code
}

If you are using several links with the same behaviour you can use a class name, not the id.

Hope it helps you

theUnforgiven's avatar

I can't do that as Barry suggested, so there must be some other way.

Shovels's avatar

Looking at your initial post, would you not be better off having a lookup table, one which simply tracks a user ID and a document ID. The stats could then be aggregated for each download, plus you could drill into each download to see which users' have accessed the file.

Shovels's avatar

I'm sure @JeffreyWay has done a video detailing the use of lookup tables - I'll have a look and see if I can find it.

theUnforgiven's avatar

I'm sure he as but after searching I can't find it, unless he named it something else.

xingfucoder's avatar

Hi @lstables, try with this schema, I made it outside any dev-env and may have errors, but try to get the idea.

linksupdatecount

You could manage visited /downloaded times differently depends on your systems. And if you prefer to avoid the use of the LinkTypes table, you could use a specific column with a list of valid values.

1 like
theUnforgiven's avatar

Cheers both, I need to think about this logically now based on the tables I have and how best to work it, but now have some idea's, thanks again.

theUnforgiven's avatar

Or could I do it so that it listens for an event using Laravel and then updates a pivot table, would that work just same?

Next

Please or to participate in this conversation.