Google Drive integration with Laravel

June 10, 2021 · 4 min read

2025 UPDATE
To fix the limitations and challenges mentioned in this article, we published two more articles: "Google Drive API Setup for Laravel 10 Integration (Part 1)" and "Laravel Project Setup for Google Drive API Integration (Part 2)." These articles give full instructions on how to set up the Google Drive API with the newest version of Laravel and how to use the Laravel Google Drive Storage package for better file handling. We strongly suggest reading these follow-up articles to learn the latest ways to do things and to solve any problems you might have with the methods talked about here.
Why Google Drive integration?
Integrating different platforms can be tough in the changing world of web development. We recently combined Laravel and Google Drive. This article will show you how to create a new file in Laravel and get its ID from Google Drive.
Google Drive uses unique IDs, not names, to recognize files. This can make getting the ID of a file made in Laravel hard. But with the right steps, you can do it.
Google Drive API setup
Before coding, you need to connect with the Google Drive API. Follow the steps in this guide for help. The guide shows how to make a new file in your Google Drive folder. If that’s all you want, you can stop here.
But if you need to upload files, get their ID, delete or change their names, keep reading. We’ll think you’ve set things up and made a simple file on Google Drive, as the guide above shows.
Coding with Laravel
We used Laravel Facades to clean up our code for this project. But to keep it simple, we won’t go into that now. All the coding will be in a controller. So, create a controller and start coding.
Imagine you’re getting a request from your view, and we’re taking that request in our store function. You need the following code to connect with Google Drive:
$client = new \Google_Client();
$client->setClientId(env('GOOGLE_DRIVE_CLIENT_ID'));
$client->setClientSecret(env('GOOGLE_DRIVE_CLIENT_SECRET'));
$client->refreshToken(env('GOOGLE_DRIVE_REFRESH_TOKEN'));
$service = new \Google_Service_Drive($client);Creating client-specific folders
In our project, we had to create a unique folder for each customer. We also wanted a main folder that was different from the main Google Drive folder. That’s why we use parents, which gets the folder’s value from the env file. We save the folderID for the customer so we can later navigate there or delete it if needed.
$fileMetadata = new \Google_Service_Drive_DriveFile([
'name' => 'your folder name',
'parents' => [env('GOOGLE_DRIVE_FOLDER_ID')],
'mimeType' => 'application/vnd.google-apps.folder'
]);
$folderId = $service->files->create($fileMetadata, [
'fields' => 'id'
]);This is how the folder structure will look after implementing the code:
GoogleDriveRootFolder/ourMainFolder/ourNewClientFolder/ourfile.jpgUploading files to client folders
After creating the client folder, we can upload our files to it. We can use a basic HTML form to upload the file, and in our case, the input name is myfile. We can skip the first line of code if we want to rename the file, but we should include it if we want to keep the original file name. We have a unique folder for each client, and that’s why we include the folderId in the parents section.
$fileName = $request->file('myfile')->getClientOriginalName();
$fileMetadata = new \Google_Service_Drive_DriveFile([
'name' => $fileName,
'parents' => [$folderId]
]);
$file = file_get_contents($request->file('myfile'));
$fileId= $service->files->create($fileMetadata, [
'data' => $file,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart',
'fields' => 'id'
]);Deleting Files from Google Drive
Our file is successfully uploaded to the folder that we created a step before. Now, if we want to delete that file, we can make use of the Laravel Storage facade, taking into consideration that you did what was suggested in the article.
Storage::disk('google')->delete($item->items['fileId']);A Note on Refresh Tokens
The refresh token provided by Google will be removed after 1 week. You must publish your app if you want to use it without the need to generate a new refresh token each week.
Conclusion
Linking Laravel to Google Drive can seem complex, but it gets much easier with a good plan. We hope this guide has made your journey smoother. Check our blog posts for more tips and real-world examples of web development.
Bring Your Ideas to Life 🚀
If you need help with a Laravel project let’s get in touch.
Lucky Media is proud to be recognized as a leading Laravel Development Agency
Technologies

Stay up-to-date
Be updated with all news, products and tips we share!

