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

xingfucoder's avatar

Hi @lstables, you could use a LinkWasDownloaded or LinkWasClicked and manage several listeners for it, whenLinkWasDownloaded or whenLinkWasClicked, and within those manage your requirements.

bashy's avatar

You can't just insert a row to, say download_tracking table and just relate those to files and have the time of the download. Then if needed, just count the amount+date via user?

theUnforgiven's avatar

I've setup this in my controller:

Event::fire('user.downloads');

then setup a app/events.php file and required that in the app > start > global.php

Then within the events.php file I have currently:

Event::listen('user.downloads', function($event){
    // Grab user Id

    // Grab Document ID

    // Add to pivot table
});

Is this the correct way and can I then use Auth::user()->id within the events.php file?

xingfucoder's avatar

If I don't remember bad, you can pass an argument within your Event fire method, this argument could be used after in the listener. Then you could pass the user there.

theUnforgiven's avatar

I've done it a slightly different way like so:

$user = Auth::user()->agent_id;

        $doc = Data::where('document', $file)->first();
        $id = $doc->folder_id;
        $folder = Folder::where('id', $id)->first();

        $file_path = public_path(). '/upload/'.$folder->folder .'/' . $doc->document;

        // Save to downloads table for tracking
        $downloads = new Downloads;
        $downloads->user_id = $user;
        $downloads->doc_id = $doc->id;
        $downloads->count++;
        $downloads->save();

        return Response::download($file_path);

I know it's a bit long winded but it works that the main thing, I can refactor later, but my question now is when I do this and pass to my view::

$agent = Auth::user()->agent_id;
$downloads = Downloads::where('user_id', $agent)->get();

return View::make('session.dashboard', compact('downloads'));

I then do a foreach loop (unless there's another way)

<table class="table">
        <tr>
            <th>File</th>
            <th>Downloads</th>
        </tr>
        @foreach($downloads as $download)
        <tr>
            <td>{{ $download->data->document }}</td>
            <td>{{ count($download->user_id) }}</td>
        </tr>
        @endforeach
        </table>

Then shows as

Which isn't right it should just show the document once and the amount of downloads.

Any advice.

cc/@bashy, @codeatbusiness

xingfucoder's avatar

Do you need to show the downloads per user? The two last files are the same?

theUnforgiven's avatar

Basically need to show something like

Section 7 - Electrical..... (shows twice yeah) instead needs to just show Section 7 Electrical .... 2 as a pose to two separate lines. same for them all really show the document once then the amount of times it's been downloaded.

argo's avatar

Shouln't you be using:

Auth::user()->agent_id->sum('doc_id');

I think that would give you the result you are looking for...

theUnforgiven's avatar

All i'm doing it getting the ID of the logged in user there. After trying you method I get Call to a member function sum() on a non-object

xingfucoder's avatar

Hi @lstables, I think would work:

Auth::user()->downloads->sum();

If you have related downloads with users with relationship methods.

Try and comment us.

theUnforgiven's avatar

I get Missing argument 1 for Illuminate\Support\Collection::sum()

theUnforgiven's avatar

Like this: Auth::user()->downloads->sum(Auth::id()); Returns 0 in the downloads column on that table I showed you.

xingfucoder's avatar

The document_id because you are getting the total of document by their ids.

theUnforgiven's avatar

Every time a doc is downloaded it adds a new row in the db so I need some code that will get the rows match the document and then add together every time it's added so that this will show on the dashboard

theUnforgiven's avatar

Oh right even so it's still showing as per previous image I posted.

argo's avatar

What about:

$downloads = DB::table('users')
                     ->Where('user_id', $agent)
                    ->orderBy('name', 'desc')
                    ->groupBy('count')
                    ->get();
Previous

Please or to participate in this conversation.