PHP code example of ikhsant / laravel-google-shared-drive

1. Go to this page and download the library: Download ikhsant/laravel-google-shared-drive library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ikhsant / laravel-google-shared-drive example snippets


use Ikhsant\LaravelGoogleSharedDrive\Models\Media;

return [
    'service_account_json' => env('GOOGLE_DRIVE_SERVICE_ACCOUNT_JSON', 'google/service-account.json'),
    'root_folder_id' => env('GOOGLE_DRIVE_ROOT_FOLDER_ID'),

    /*
     * The model that should be used for storing media records.
     */
    'media_model' => Media::class,

    /*
     * The default disk name stored in the media table.
     */
    'disk' => 'google_drive',
];

use Ikhsant\LaravelGoogleSharedDrive\Traits\HasMedia;
use Illuminate\Database\Eloquent\Model;

class Consultation extends Model
{
    use HasMedia;
}

// Upload a file to a nested folder 'consultations/docs', customize filename, and attach to 'attachments' collection
$media = $consultation->addMedia($request->file('file'))
    ->toFolder('consultations/docs')
    ->usingFileName('custom-name.pdf')
    ->toMediaCollection('attachments');

$media = $consultation->media()->first();

// Get the raw file content from Google Drive
$contents = $media->contents();

return response($contents)
    ->header('Content-Type', $media->mime_type)
    ->header('Content-Disposition', 'attachment; filename="'.$media->file_name.'"');

$media->delete(); // This automatically calls GoogleSharedDrive::delete()

use Ikhsant\LaravelGoogleSharedDrive\Facades\GoogleSharedDrive;

$result = GoogleSharedDrive::upload($request->file('file'), 'custom/folder/path');

// Response:
// [
//     'file_id' => '...',
//     'file_name' => '...',
//     'mime_type' => '...',
//     'size' => 12345
// ]

use Ikhsant\LaravelGoogleSharedDrive\Facades\GoogleSharedDrive;

$contents = GoogleSharedDrive::download($fileId);

use Ikhsant\LaravelGoogleSharedDrive\Facades\GoogleSharedDrive;

GoogleSharedDrive::delete($fileId);
bash
php artisan migrate
bash
php artisan vendor:publish --tag=google-shared-drive-config