munyamakudzai095's avatar

Upload files to Firebase Storage

public function store(Request $request)
    {
      try {
        $request->validate([
            'img_url' => 'nullable|mimes:jpg,jpeg,gif,webp,png',
            'name' => 'required',
            'content' => 'required'
        ]);

        

        $image = $request->file('img_url'); //image file from frontend  
        $img   = app('firebase.firestore')->database()->collection('images')->document('defT5uT7SDu9K5RFtIdl');  
        $firebase_storage_path = 'images/';  
        $name     = $img->id();  
        $localfolder = public_path('firebase-temp-uploads') .'/';  
        $extension = $image->getClientOriginalExtension();  
        $file      = $name. '.' . $extension;  
        if ($image->move($localfolder, $file)) {  
            $uploadedfile = fopen($localfolder.$file, 'r');  
            app('firebase.storage')->getBucket()->upload($uploadedfile, ['name' => $firebase_storage_path . $name]);  
            //will remove from local laravel folder  
            unlink($localfolder . $file);  
            Session::flash('success', 'Image uploaded successfully.');
        } else {  
          Session::flash('error', 'Image uploaded successfully.'); 
        } 

        $firebaseStorage = app('firebase.storage');

        // Get reference to uploaded file
        $fileRef = $firebaseStorage->getBucket()->object($name); 

        // Get public download URL 
        $imageUrl = $fileRef->signedUrl();

        Testimony::create([
            'img_url' => $imageUrl,
            'name' => $request->title,
            'content' => $request->content
        ]);

        return redirect()->back()->with('success', 'Resource created.');
      } catch (\Throwable $th) {
        return redirect()->back()->with('error', 'Internal server error.' . $th->getMessage());
      }
    }

I want to post an image to firebase storage and text to mysql database

I get the error

Target class [firebase.firestore] does not exist.
0 likes
4 replies
jmtvrs's avatar

Hi there.

That error is being thrown by app('firebase.firestore'), but to understand why we need more details, like what package are you using to interact with Firebase?

Assuming you are using kreait/laravel-firebase, have you executed:

# Laravel
php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config
munyamakudzai095's avatar

@jmtvrs I am using kreait/laravel-firebase and I have executed the following

php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config

and the response is

   INFO  No publishable resources for tag [config]. 
jmtvrs's avatar

@munyamakudzai095

It looks like something went wrong during the installation process. Did you follow the installation?

  1. Try to remove the package composer remove kreait/laravel-firebase and re-add it following the instructions above.
  2. Use php artisan vendor:publish to get a list of the available tags. Check if the config is available. it is possible that you can get the same result just by using: php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider"
  3. Create a copy of the firebase.php file inside the config folder manually. It's not ideal, but it could help you move forward.
1 like
munyamakudzai095's avatar
Level 1
<?php

namespace App\Http\Controllers;

use App\Models\Uploads;
use Illuminate\Http\Request;
use Kreait\Firebase\Storage\Bucket;

class FirebaseCrudController extends Controller
{
    public function index()
    {
        return view('pages.upload');
    }

    /**
     * The Upload function 
     * 
     */
    public function upload(Request $request)
    {
        try {
            $request->validate([
                'img_url' => 'required',
                'content' => 'required'
            ]);

            if ($request->hasFile('img_url')) {
                // Retrieve the file from the request
                $file = $request->file('img_url');

                // Create an instance of the Firebase Storage client
                $firebase = app('firebase.storage');
                $storage = $firebase->getBucket();

                // Set the desired storage location and filename
                $storagePath = 'images/';
                $filename = $file->getClientOriginalName();

                // Upload the file to Firebase Storage
                $upload = $storage->upload(
                    fopen($file->getRealPath(), 'r'),
                    [
                        'predefinedAcl' => 'publicRead',
                        'name' => $storagePath . $filename,
                    ]
                );

                // Get the public URL of the uploaded file
                $publicUrl = $upload->info()['mediaLink'];

                // Return the public URL to the uploaded file
                Uploads::create([
                    'content' => $publicUrl
                ]);

                return redirect()->route('display')->with('success', 'zvaita');
            } else {
                return redirect()->back()->with('error', 'No file was uploaded.');
            }
        } catch (\Throwable $th) {
            return redirect()->back()->with('error', 'error uploading. ' . $th->getMessage());
        }
    }

    public function display()
    {
        $uploads = Uploads::all();
    return view('pages.display', ['uploads' => $uploads]);
    }
}

Thank you guys for the help and insights..

This finally works. Hopefully it helps someone else.

Please or to participate in this conversation.