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

Sunday, February 20, 2022

[FIXED] Laravel - Creating tables dynamically (without migration)

 February 20, 2022     database-schema, laravel, mysql, php     No comments   

Issue

I'm trying to create a table dynamically upon an admin request, and while it seems it should be straightforward like most of Laravel's documentation, I can't seem to create a table. Though I can drop tables, and add or drop columns as I wish.

This is my basic code model:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

public static function createTable()
{
    Schema::create('randomName', function($table) {
        $table->increments('id');
        $table->string('name');
    });
}

What could possibly be the problem here? Unfortunately I don't receive any errors so not sure how to debug it.

Any advice? Thanks in advance!


Solution

Ugh, never mind... I worked on it for long enough, and the solution as always was... Very simple.

I just had to figure a connection for the database first, so instead of

Schema::create('tableName', function($table)
{           
    $table->increments('id');
});

It is

Schema::connection('mysql')->create('tableName', function($table)
{
    $table->increments('id');
});

Hope this helps someone someday in the future!



Answered By - Shay
  • 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