This episode is for members only

Sign up to access "Build a Livewire CSV Importer/Exporter" right now.

Get started
Already a member? Sign in to continue
Playing
20. Storing exports per user

Transcript

00:00
Typically when I'm working with applications where I'm generating and storing files in the private directory, I will almost always split these up so they are per user. Now what do I mean by that? Well I mean that instead of just dumping these transactions in here which could potentially
00:17
conflict at some point, we're going to split these up and put them inside of a specific user folder per user. That means that we're never really going to end up with a duplicate because we split it out into another section. So let's go ahead and delete the two transactions that we have from here. Let's open up the exports table and get rid of this from here as well.
00:39
So we're pretty much starting fresh with our exports and then we're really not going to make this any more complicated than it needs to be. Pretty much at the point of storing and accessing this export, we're going to prefix this with the user's ID. Now you can change this up to be anything. You can even change the name of the file to be a randomly generated string if you preferred
01:02
but I'd like to keep this file with that same structure but I just want to split this up. So where we go ahead and queue this, we know that we pass in the exporter which gives us all of that information. Let's just pull these down onto individual lines just so it's a little bit easier to manage here and just before export file we're not going to do this for the actual file that
01:22
we're storing here although we could because then technically it would be stored in the database. We're going to prefix this for the user's ID. The reason I like to do this separately is because the file I want to say well that's the file name but the location of this is going to be different so I like to keep these things separated although you could combine them. So to grab the user's ID
01:44
that's pretty straightforward. We can use the auth helper and ID. We can go ahead and prefix the folder name on here that we want to store this under and there we go. Now we're not quite done but let's just make sure that this gets stored first of all. So let's go over to our transactions let's export 10 of these records here and just wait for that to finish. Great. Now download here
02:06
is not going to work. If we click on that you can see we get an error here. The reason for this is just because we are accessing the wrong file. So let's go over to our web routes remind ourselves where we have this download that's in this export controller and you can see here we are just referencing the file itself. Now of course that's not the case anymore so we can either use auth id
02:30
here or you can use the request object to grab this. We're going to do this forward slash export file and we should now be able to download that file. So a really simple change but it's important to make sure that we split these out into these individual structures. First of all it makes it really easy to do a tidy up if a user deletes their account and we want to delete these files
02:53
alongside of this and second of all it avoids any conflicts in names. If two people were exporting the same thing at the exact same time we wouldn't get a conflict and that's really important because we don't want any data to be accessible between different users. So feel free to tweak this but it's really just a tip I like to split these out just in case.
22 episodes2 hrs 18 mins

Overview

Let's build a powerful CSV importer and exporter with Livewire, completely from scratch.

This can handle millions of rows, be reused for multiple models, and by using queues, doesn't require the browser to be open.

This course is for you if:

  • You need a robust drop-in importer and exporter component for your models that you have full control over
  • You want to brush up on some advanced Livewire concepts
  • You want to learn about queues in Laravel
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!