Home > Software engineering >  How to store sheets data to mysql in laravel
How to store sheets data to mysql in laravel

Time:06-16

I have been trying to learn how to use the Sheetdb api today seems easy but was wondering how would I take the sheets data and insert it to a mysql table. Like how would I do it and what would I need. Here is code in the controller right now:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use SheetDB\SheetDB;

class SheetdbController extends Controller
{
public function get(){
    $sheetdb= new SheetDB('5wwy7qkrnqxt1');
    $result = $sheetdb->get();
    
    if($result){
        dd($result);
        foreach($result as $row){
            DB::table('DbSheets')->insert([
                'name' => $row->name
            ]);
        }
    }
}
}

Table migration:

public function up()
{
    Schema::create('DbSheets', function (Blueprint $table) {
        $table->id();
        $table->string('name', 255)->nullable();
        $table->timestamps();
    });
}

Here is the output:

enter image description here

So the data I get back looks like and array of objects so how would I insert this data to and existing empty mysql table? Any and all suggestions are appreciated. If you wish to recreate the whole project here is a link to a video for the initial setup https://www.youtube.com/watch?v=qaWc7Nlt2EE.

CodePudding user response:

Hello I an attaching snippet

$result = $sheetdb->get();
if($result){
    foreach($result as $row){
        DB::table('table_name')->insert([
            'id' => $row->Id,
            'name' => $row->name
        ]);
    }
}
  • Related