Connection Firebase Firestore to laravel - laravel

I have installed laravel 5.8 and firebase project with firestore database.
"name": "laravel/framework",
"version": "v5.8.36",
Firestore database connected to android app. App fetches data good from Firestore. Than I want to create admin panel with laravel for android app and want to integrate laravel with that database.But can not do this.
What I did:
installed php7.2
installed laravel 5.8.*
added php extension gRPC*
Added gRPC as a Composer dependency composer require "grpc/grpc:^v1.1.0" in laravel project
installed composer require google/cloud-firestore
Generated Firebase Admin SDK json file and saved into storage folder in laravel
Added this variable GOOGLE_APPLICATION_CREDENTIALS=/storage/files/progressive-yooung-team-firebase-adminsdk-ax7wi-d2a85ecabc.json (json file which generated firebase admin SDK) in .env file of laravel
installed composer require kreait/firebase-php ^4.35
Created Controller 'VarController' code:
<?php
namespace App\Http\Controllers;
use Kreait\Firebase\Factory;
class VarController extends Controller
{
public function index()
{
print_r("Output: 1");
$factory = new Factory();
print_r("Output: 2");
$firestore = $factory->createFirestore();
print_r("Output: 3");
$database = $firestore->database();
$userRef = $database->collection('users');
$snapshot = $userRef->document('Hus')->snapshot();
if($snapshot->exists()) {
printf('Document data:' . PHP_EOL);
print_r($snapshot->data());
}
print_r("Output: 4");
}
}
Problem is, It does not fetch data from firestore document 'Hus' and its data exists:
users > Hus > name: "Husniddin"
I put print_r("Output: 1"), Output: 2, etc... in order to know where is problem. On screen I see only: Output: 1 Output: 2.

Also don't forget to import these .
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
use \Kreait\Firebase\Database;
use Google\Cloud\Firestore\FirestoreClient;
After that , call the ServiceAccount() inside your function.
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/Firebase.json');
$firebase = (new Factory)
->withServiceAccount($serviceAccount);
$firestore = new FirestoreClient([
'projectId' => 'Your project name',
]);
$collectionReference = $firestore->collection('users');
$documentReference = $collectionReference->document('Search element from document');
$snapshot = $documentReference->snapshot();

change
$snapshot = $userRef->document('Hus')->snapshot();
to
$snapshot = $userRef->document('Hus')->documents();

Related

Permission denied on storage/app/all_html/1658920379.blade.php. The communication protocol is not supported

Hi I just want to download my storage file to pdf but it show this error.
Here is my Controller
public function html(Request $request, $id=null){
if($request->isMethod('post')){
$html = $request->html;
$data = [];
if($request->hasFile('html')){
// dd($html);
$data['html'] = $request->file('html')->storeAs('all_html', time().'.blade.php');
// $data['html'] = Storage::putFile('all_html', $html);
}
$url = "storage/app/".$data['html'];
$random = 'dOC'.str::random('10');
Pdf::loadfile($url)->setPaper('a4', 'landscape')->setWarnings(false)->save(public_path('pdf/'.$random.'.pdf'));
return response()->download(public_path('pdf/'.$random.'.pdf'));
}
return view('html');
}
You need to check first in laravel storage link or not if the storage is not linked then you need to run a below command in your terminal
Storage Link command => php artisan storage:link
then check our code and run again
If this not working then you need to check your laravel storage folder permission and give them to permission
Happy Coding!!

Laravel Spatie Activity Log Ip Address Customize

I am using Laravel Spatie Activity Log package in my laravel application.
I could customize in each module.
public function tapActivity(Activity $activity, string $eventName)
{
$activity->description = "Category is {$eventName}. ";
$activity->os = getOS();// custom hlper
}
This problem was when I was going to save ip.
$activity->ip = $request->ip();
I think above field should be added.
But where can we get $request ?
I solved this myself.
$activity->ip = request()->ip();
This just worked. :)

Create custom cache drive in laravel 5.8 given error "Driver [aerospike] is not supported."

I wanted to create a custom cache driver using aerospike.
I have followed the instruction given in the document:
https://laravel.com/docs/5.8/cache#adding-custom-cache-drivers
Cache::extend('aerospike', function ($app) {
$config = $app['config'];
$server = $config['cache.stores.aerospike.servers'];
$aerospike = new \Aerospike($server);
$store = new AerospikeStore($aerospike, $config['cache.prefix'], $config['cache.stores.aerospike.namespace']);
return Cache::repository($store);
});
also created the AerospikeStore file.
When run php artisan serve it always says :
In CacheManager.php line 109:
Driver [aerospike] is not supported.

Class 'Dropbox\Client' not found ( Laravel )

I'm use Dropbox image hosting, I downloaded Dropbox's install league / flysystem-dropbox package, but when I ran the code below it crashed
Class 'Dropbox \ Client' not found.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Dropbox\Client;
use Dropbox\WriteMode;
class ExpenseController extends Controller
{
public function postExpenseAdd( Request $request ){
$Client = new Client(env('DROPBOX_TOKEN'), env('DROPBOX_SECRET'));
$file = fopen(public_path('img/admin.png'), 'rb');
$size = filesize(public_path('img/admin.png'));
$dropboxFileName = '/myphoto4.png';
$Client->uploadFile($dropboxFileName,WriteMode::add(),$file, $size);
$links['share'] = $Client->createShareableLink($dropboxFileName);
$links['view'] = $Client->createTemporaryDirectLink($dropboxFileName);
print_r($links);die;
}
}
This will not be an answer to your current question (at least not with the package you are currently working with), but this might get you back on the right track:
The league/flysystem-dropbox won't work anymore as this package uses the API v1 version of dropbox that has meanwhile been deprecated (API v1 will be completely unavailable on September 28, 2017).
Since the package is not maintaned anymore and will not receive an update to the API v2 version you should take a look at the srmklive/flysystem-dropbox-v2 package.
In short: you can run composer require srmklive/flysystem-dropbox-v2 to get started with the new version of the API.

Datatable server side php class for laravel

I want my datatables to process the data on the server side, I am referencing this example.
Server Side Example
However, the server side php class "ssp.class.php" given in this example uses core php with raw sql, I can not use it directly for laravel projects. Does anyone has reference to laravel way doing datatables. I don't want to use any packages at the moment though.
UPDATED 31/01/2023
You can use syamsoul/laravel-datatable-two-ssp package..
Link here
Open CLI and enter your app directory
Run this command composer require syamsoul/laravel-datatable-two-ssp
In your controller, need to add use SoulDoit\DataTableTwo\SSP;, and you can follow the code below:
private function dtColumns()
{
return [
['label'=>'ID', 'db'=>'id', 'formatter'=>function($value){
return str_pad($value, 8, '0', STR_PAD_LEFT);
}],
['label'=>'Email', 'db'=>'email' ],
['label'=>'Username', 'db'=>'username' ],
['label'=>'Created At', 'db'=>'created_at' ],
];
}
private function dtQuery($selected_columns)
{
return \App\Models\User::select($selected_columns);
}
This is working perfectly on Laravel 8 and above.
.
.
Below is my old answer.. (21 March 2019)
You can use syamsoul/laravel-datatable-ssp package..
Link here
Open CLI and enter your app directory
Run this command composer require syamsoul/laravel-datatable-ssp
In your controller, need to add use SoulDoit\DataTable\SSP;, and you can follow the code below:
public function get(){
$dt = [
['db'=>'id', 'dt'=>0, 'formatter'=>function($value, $model){ return str_pad($value, 8, '0', STR_PAD_LEFT); }],
['db'=>'email', 'dt'=>1],
['db'=>'first_name', 'dt'=>2, 'formatter'=>function($value, $model){ return $value . ' ' . $model->last_name; }],
['db'=>'created_at', 'dt'=>3],
['db'=>'email_verified_at'],
['db'=>'last_name'], // must include this because need to re-use in 'first_name' formatter
];
$dt_obj = new SSP('\App\User', $dt);
$dt_arr = $dt_obj->getDtArr();
return response()->json($dt_arr);
}
This is working perfectly on Laravel 5.8 ... I'm not sure about the other versions.

Resources