{"id":25,"date":"2021-01-14T07:39:31","date_gmt":"2021-01-14T07:39:31","guid":{"rendered":"https:\/\/mouryasolutions.in\/blog\/?p=25"},"modified":"2023-01-29T18:33:37","modified_gmt":"2023-01-29T18:33:37","slug":"create-crud-in-laravel-with-firebase","status":"publish","type":"post","link":"https:\/\/mouryasolutions.in\/blog\/create-crud-in-laravel-with-firebase\/","title":{"rendered":"Create CRUD in Laravel with Firebase Realtime Database"},"content":{"rendered":"<div class=\"post-content\">\n<blockquote class=\"wp-block-quote\">\n<p>To perform CRUD (Create, Read, Update, Delete) operations in Laravel with Firebase Realtime Database, you can use the official Firebase PHP SDK. Here is an example of how to perform CRUD operations in Laravel with Firebase Realtime Database:<\/p>\n<\/blockquote>\n\n\n\n<h4>First, install the Firebase PHP SDK:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require firebase\/php-jwt\r\ncomposer require kreait\/firebase-php\r<\/code><\/pre>\n\n\n\n<h4>Next, you can use the following code to connect to Firebase Realtime Database:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>use Kreait\\Firebase\\Factory;\r\nuse Kreait\\Firebase\\ServiceAccount;\r\n\r\n$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'\/service-account.json');\r\n$firebase = (new Factory)\r\n    ->withServiceAccount($serviceAccount)\r\n    ->create();\r\n\r\n$database = $firebase->getDatabase();\r\n<\/code><\/pre>\n\n\n\n<p>Note: Replace <code>service-account.json<\/code> with the path to your Firebase Service Account JSON file.<\/p>\n\n\n\n<h4>Create a new record:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$newPost = $database\r\n    ->getReference('posts')\r\n    ->push(&#91;\r\n        'title' => 'My first post',\r\n        'body' => 'Hello, Firebase!'\r\n    ]);\r\n<\/code><\/pre>\n\n\n\n<h4>Read a record:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$post = $database\r\n    ->getReference('posts\/'.$postId)\r\n    ->getValue();\r\n<\/code><\/pre>\n\n\n\n<p>Note: Replace <code>$postId<\/code> with the actual ID of the post that you want to read.<\/p>\n\n\n\n<h4>Update a record:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$database\r\n    ->getReference('posts\/'.$postId)\r\n    ->update(&#91;\r\n        'title' => 'My updated post',\r\n        'body' => 'Hello, updated Firebase!'\r\n    ]);\r\n<\/code><\/pre>\n\n\n\n<p>Note: Replace <code>$postId<\/code> with the actual ID of the post that you want to update.<\/p>\n\n\n\n<h4>Delete a record:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$database\r\n    ->getReference('posts\/'.$postId)\r\n    ->remove();\r\n<\/code><\/pre>\n\n\n\n<p>Note: Replace <code>$postId<\/code> with the actual ID of the post that you want to delete.<\/p>\n\n\n\n<p>This is a basic example of how to perform CRUD operations in Laravel with Firebase Realtime Database. You can customize it to fit your needs.<\/p>\n<\/div><!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>To perform CRUD (Create, Read, Update, Delete) operations in Laravel<a href=\"https:\/\/mouryasolutions.in\/blog\/create-crud-in-laravel-with-firebase\/\">Read More<i class=\"fa fa-long-arrow-right\" aria-hidden=\"true\"><\/i><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,11],"tags":[],"_links":{"self":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/25"}],"collection":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":7,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":489,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions\/489"}],"wp:attachment":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}