PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 19, 2022

[FIXED] How to add content from Laravel to Firestore?

 April 19, 2022     firebase, google-cloud-firestore, laravel, php     No comments   

Issue

Adding content from Laravel to Firebase database as follows:

$postRef = $this->database->getReference($this->tablename)->push($postData);

But I don't know how to add content from Laravel to Firestore. This is my Firestore: enter image description here

This is how Laravel looks like:

These are my codes:

<?php

namespace App\Http\Controllers\Firebase;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Kreait\Firebase\Contract\Firestore;

class ContactController extends Controller
{
    public function __construct(Firestore $firestore)
    {
        $this->firestore = $firestore;
        $this->tablename = 'kategoriler';
    }

    public function index()
    {
        return view('firebase.contact.index');
    }
    public function create()
    {
        return view('firebase.contact.create');
    }
    public function store(Request $request)
    {
        $postData = [
            'comment' => $request->comment,
            'iD' => $request->iD,
            'imgUrl' => $request->imgUrl,
            'lat' => $request->lat,
            'location' => $request->location,
            'lon' => $request->lon,
            'name' => $request->name,
            'youtubeId' => $request->youtubeId,

        ];

        $postRef = $this->app('firebase.firestore')->database()->collection($this->tablename)->Document(0)->collection('bolgeler')->push($postData);
        if($postRef)
        {
            return redirect('contacts')->with('durum','İçerik eklendi.');
        }
        else
        {
            return redirect('contacts')->with('durum','İçerik eklenemedi.');
        }

Solution

If you want add data to bolgeler collection with auto document name, you can do this :

$postRef = $this->app('firebase.firestore')->database()->collection('bolgeler')->newDocument()->set($postData);

or :

$posref = $this->firestore->database()->collection('bolgeler')->newDocument()->set($postData);

when you nedd add specific name :

$postRef = $this->app('firebase.firestore')->database()->collection('bolgeler')->document('id001')->set($postData);

or:

$posref = $this->firestore->database()->collection('bolgeler')->document('id001)->set($postData);


Answered By - Syaifudin Zuhri
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing