Hi @lstables, you could use a LinkWasDownloaded or LinkWasClicked and manage several listeners for it, whenLinkWasDownloaded or whenLinkWasClicked, and within those manage your requirements.
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?
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?
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.
Ok thought so.
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.
Do you need to show the downloads per user? The two last files are the same?
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.
Database is structured like:
id
user_id
doc_id
Shouln't you be using:
Auth::user()->agent_id->sum('doc_id');
I think that would give you the result you are looking for...
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
Hi @lstables, I think would work:
Auth::user()->downloads->sum();
If you have related downloads with users with relationship methods.
Try and comment us.
I get Missing argument 1 for Illuminate\Support\Collection::sum()
Enter the id field within the sum method.
Like this: Auth::user()->downloads->sum(Auth::id());
Returns 0 in the downloads column on that table I showed you.
The document_id because you are getting the total of document by their ids.
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
Still shows like the image I posted
Try groupBy and sum after by the documentId column.
Sorry i dont follow you, how do you mean?
hi @lstables, see the following links:
Aggregate functions
http://laravel.com/docs/master/queries#aggregates
Selects - Grouping
Oh right even so it's still showing as per previous image I posted.
What about:
$downloads = DB::table('users')
->Where('user_id', $agent)
->orderBy('name', 'desc')
->groupBy('count')
->get();
Please or to participate in this conversation.