Laravel, copy tables from one database to other - laravel

I'm working on a SaaS project. With different client requests for more fields or less etc, i believe it would be helpful instead of just keep growing on already created tables, divide the tables that could indeed grow into separate databases per client. To do so, it would involve recreating the tables to X connection which is already set, and creating a bunch of tables making migrations huge.
My question is if there is a method to copy table example_table from database A to database B keeping the data using the normal migrations, or if i would be better off using some DBMS to make the templates.

You can do this with a raw query
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use DB;
class MyNewTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
DB::statement('CREATE TABLE conection2.newtable LIKE system.oldtable; ');
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::connection('conection2')->drop('newtable');
}
}

Related

Wherenull query doesn't return anything from pivot table (laravel5.5)

I have crime_criminal table that has this
With regards to that this is my CrimeCriminalSeeder
<?php
use Illuminate\Database\Seeder;
use App\CrimeCriminal ;
class CrimeCriminalSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
// seeding criminal_description table..
$faker = \Faker\Factory::create();
$crimes = CrimeCriminal::whereNull('crime_description')->get();
dd($crimes);
}
}
but I get like this in my terminal
Potentially the column stating (null) may contain text? Otherwise seems correct.

Laravel Many-to-Many Pivot Table

I have a fairly simple question about the use of Pivot tables in laravel. Firstly ill give some information about my situation, I have two tables name "Vehicles", and "Events". Now I would like to create a table that will be used to hold vehicles that have registered for an event. Now the relationship that between these two tables would be that "Many Vehicles can register for Many Events" and vice versa. would a Pivot table be the best way to accomplish this, and if so could more singular values be in the same table?
You can associate an event with multiple vehicles and a vehicle to multiple events by doing something like this with your models (not tested):
Vehicle.php
<?php
namespace App;
use App\Event;
use Illuminate\Database\Eloquent\Model;
class Vehicle extends Model
{
...
/**
* Get the events that this vehicle belongs to.
*
* #return \App\Event
*/
public function events()
{
return $this->belongsToMany(Event::class, 'vehicle_event');
}
}
Event.php
<?php
namespace App;
use App\Vehicle;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
...
/**
* Get the vehicles that this event has.
*
* #return \App\Vehicle
*/
public function events()
{
return $this->hasMany(Vehicle::class, 'vehicle_event');
}
}
You'll also need a migration file for the pivot table:
...
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('vehicle_event', function(Blueprint $table)
{
$table->integer('vehicle_id')->unsigned()->index();
$table->foreign('vehicle_id')->references('id')->on('vehicles');
$table->integer('event_id')->unsigned()->index();
$table->foreign('event_id')->references('id')->on('events');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('vehicle_event');
}
...
Then you can to use attach() and detach() to associate vehicles with events or vice versa.

neo4j migration in laravel doesnot work properly

I am using Neo4j in my application with /Vinelab/NeoEloquent. Now the problem is that when I run php artisan neo4j:migrate --database=neo4j
, node for the migration table is created.
Not any labels defined in function up() is not created. What should I do to solve this?
Schema class is,
<?php
use Vinelab\NeoEloquent\Schema\Blueprint;
use Vinelab\NeoEloquent\Migrations\Migration;
class CreateTestpostsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Neo4jSchema::label('Testposts', function(Blueprint $label)
{
$label->unique('tpid');
$label->index('name');
$label->index('content');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Neo4jSchema::drop('Testposts');
}
}
According to your Schema code, no labels should be expected to be created. You are setting a constraint on the tpid to be unique, and indexing the name and content of the nodes with Testposts label.
To check if your migration has taken effect:
Try creating two nodes with the same tpid and you should get an error
Try profiling the queries to the name and content properties, this link should help with that: http://neo4j.com/docs/stable/cypherdoc-basic-query-tuning-example.html
On a side note, if the Label of the node in your model is Post or anything other than Testposts, then you should change Testposts to whatever is in your model for this to work properly.

Laravel 5.0 - Rename multiple columns in 1 migration

We recently made a switch of service provider such that multiple columns in a DB table for this project will need to be renamed.
I am aware of this post which shows how to rename 1 column from 1 table:
php artisan migrate:make rename_stk_column --table="YOUR TABLE" --create
Is there a way to execute this same migration with multiple columns ? (1 migration, not more than 1...trying to minimize number of migration files created)
You can just add multiple renameColumn(); statements for each column that needs to be updated in that given table. Just need to come up with a whatever name you guys/gals use for your migration files.
Just a sample of what I ran
class MultipleColumnUpdate extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function ($table) {
$table->renameColumn('name', 'user_name');
$table->renameColumn('email', 'work_email');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function ($table) {
$table->renameColumn('user_name', 'name');
$table->renameColumn('work_email', 'email');
});
}
}

No relations updated in the DB after writing symfony/doctrine associations

So I am working on a project in symfony 3.0.1. I have generated entities (via doctrine:generate:entity with the following interactive setup questions) and then did the doctrine:schema:update --force to generate the tables in the DB from the Doctrine entities.
I then went in the entities and created associations.
After that I went ahead and did another doctrine:schema:update -- force and after checking in phpMyAdmin no relations were created by the schema update I forced.
Is there something else I am missing? See below my entities:
namespace MyAppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Logins
*
* #ORM\Table(name="logins")
* #ORM\Entity(repositoryClass="MyAppBundle\Repository\LoginsRepository")
*/
class Logins
{
/**
* #var int
*
* #ORM\Column(name="RoleID", type="integer")
* #ORM\ManyToOne(targetEntity="Roles", inversedBy="roleID")
* #ORM\JoinColumn(name="roleID", referencedColumnName="roleID")
*/
private $roleID;
'
'namespace MyAppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Roles
*
* #ORM\Table(name="roles")
* #ORM\Entity(repositoryClass="MyAppBundle\Repository\RolesRepository")
*/
class Roles
{
/**
* #var int
*
* #ORM\Column(name="roleID", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\OneToMany(targetEntity="Logins", mappedBy="roleID")
*/
private $roleID;
public function __construct() {
$this->roleID=new ArrayCollection();
Still not sure why there were conflicts, but I resolved my problem with Doctrine reverse engineering. I created the relations in PMA and then generated mapping from it. All worked out well.
EDIT: The guy in comments to my post was right. I cannot assign One-to-Many on an Identity property.
I have had frustrating problems with relations not updating on my production environment several times now. My code would execute as expected with no errors, but the table(s) simply wouldn't update.
I've discovered that if you are using APC and you have just added a new relationship, you need to restart the web server in order for old versions of the entity classes to be flushed out.

Resources