2

I'm new to Laravel and am trying to this extension: https://github.com/kawax/laravel-google-sheet to read data from a Google Spreadsheet.

However, the documentation doesn't spell out each step.

I realised I had to put the provider in app.php like this: 'Sheets' => Revolution\Google\Sheets\Sheets::class,, and an alias in my app.php like this Sheets' => Revolution\Google\Sheets\Sheets::class, which isn't in the documentation. Hopefully that's right...?

I then tried the first Laravel example from README.md

use Sheets; // I had to precede with "\"
use Google; // I had to precede with "\"

Sheets::setService(Google::make('sheets'));
Sheets::spreadsheet('myspreadsheetid');

However I get this error:

Non-static method Revolution\Google\Sheets\Sheets::setService() should not be called statically
Samuurai
  • 375
  • 2
  • 13

1 Answers1

5

I'm not familiar with the library.

However, it looks like your Sheets class that you call the static setService() method on is not a facade for an actual Sheets instance.

Try

use Revolution\Google\Sheets\Facades\Sheets;

Instead of

use \Sheets;
Stefan
  • 1,325
  • 12
  • 25
  • That worked! Thank you so much! How did you know to use that namespace? – Samuurai Sep 11 '17 at 19:31
  • They followed the Laravel Facade pattern. The facade allows you to apparently call methods statically without instantiating the actual object that implements the methods or having an instance injected by the DIC. I checked their source code to see where the facade is defined: [here](https://github.com/kawax/laravel-google-sheets/tree/master/src/GoogleSheets/Facades). For more on facades see [here](https://laravel.com/docs/5.5/facades). – Stefan Sep 12 '17 at 05:14
  • Thanks a lot! Beginner question here... is that better because it saves memory not having to load the entire class? – Samuurai Sep 12 '17 at 15:41
  • Memory is not the question here. Eventually, this facade creates an instance of \Revolution\Google\Sheets\Sheets and delegates the static method calls to it. This class name is provided to Laravel by the facade's getFacadeAccessor method. Using a facade or not comes down to coding preference and requirements such as testability. Facades bind you to a concrete implementation, while you could swap it when using [dependecy injection](https://stackoverflow.com/questions/130794/what-is-dependency-injection). See [facades vs. dependency injection further down](https://laravel.com/docs/5.5/facades). – Stefan Sep 12 '17 at 16:15